0

我有 aitisi(id、name1、surname1、father1、birthdate1、name2、surname2、father2、birthdate2... 到 5、cost、begins、expires、plan、address、phone、city、zip),我想复制 surnameX , nameX,fatherX,birthdateX (X= 1 to 5) 列到新表成员 (id, tid, name, surname, Father,birthdate) 其中 tid 是 table1.id 的查找键

我用 MySQL 试过,但没有做对,所以我最后用 PHP 做了 id:

$query_aitisi = "SELECT * FROM aitisi order by id asc";
$aitisi = mysql_query($query_aitisi, $connection) or die(mysql_error());
$row_aitisi = mysql_fetch_assoc($aitisi);
$totalRows_aitisi = mysql_num_rows($aitisi);

do {
  for($i=1; $i<=5; $i++){
    if ($row_aitisi['on_te'.$i]<>'') { 
    $query_copy = "insert into members (symbid, name, surname, father, birthdate) 
    values ('".$row_aitisi[id]."','".$row_aitisi['surname'.$i]."','".$row_aitisi['name'.$i]."','".$row_aitisi['father'.$i]."','".$row_aitisi['birthdate'.$i]."')";
$copy = mysql_query($query_copy, $connection) or die(mysql_error());
    }
  }
} while ($row_aitisi = mysql_fetch_assoc($aitisi));

但是,我真的很想知道正确使用“create table as select”语句以供将来使用。有人吗?谢谢!

4

1 回答 1

0

以下查询应该可以正常工作

create table members (symbid, surname, name, father, birthdate)
    (select id, surname1, name1, father1, birthdate1 from aitisi where on_te1 != ''
    union all
    select id, surname2, name2, father2, birthdate2 from aitisi where on_te2 != ''
    union all
    select id, surname3, name3, father3, birthdate3 from aitisi where on_te3 != ''
    union all
    select id, surname4, name4, father4, birthdate4 from aitisi where on_te4 != ''
    union all
    select id, surname5, name5, father5, birthdate5 from aitisi where on_te5 != '')
于 2012-06-27T07:31:37.777 回答