【问题标题】:Copy data between tables using create table with select使用 create table with select 在表之间复制数据
【发布时间】:2012-06-27 07:25:36
【问题描述】:

我有 aitisi(id、name1、surname1、father1、birthdate1、name2、surname2、father2、birthdate2... 到 5、成本、开始、到期、计划、地址、电话、城市、邮编),我想将 surnameX、nameX、fatherX、birthdateX(X=1 到 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”语句以供将来使用。有人吗?谢谢!

【问题讨论】:

    标签: mysql


    【解决方案1】:

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

    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 != '')
    

    【讨论】:

    • 这只是将整个表复制到一个新表中。恐怕不是我要找的东西。
    • 我把你的问题完全弄错了 :) 我已经更新了我的答案。
    • 没有太多时间来完全按照我的需要进行修改,但第一次尝试给了我一个错误。我会更仔细地调查并及时通知您。
    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 2013-06-29
    • 2019-06-26
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多