MySQL将1行以上的行移到另一个表中,所有这些都在1个语句中?

时间:2013-04-24 14:12:10

标签: mysql

所以我需要将从这个语句输出的所有行插入到单独的表中:

    SELECT * FROM children WHERE quoteid = '$quoteid'

这只会插入其中一行:

    INSERT INTO beneficiaries (quoteid, uid, name, percent) VALUES('$quoteid', '$uid', '$nm', '10')

任何人都知道如何移动多行或者我必须单独发送它们吗?

更新:

之前应该提到过这个,但是没有,'children'没有相同的列名,并且更改它们不是一个选项。遗憾!

$query = mysql_query("SELECT * FROM beneficiaries WHERE quoteid = '$quoteid'");
$getchildrenrid2 = mysql_fetch_assoc($query);

if(mysql_num_rows($query) == 0)
{
    $getchildren = mysql_query("SELECT * FROM children WHERE quoteid = '$quoteid'");
    $getchildrenrid = mysql_fetch_assoc($getchildren);

        $nm = $getchildrenrid['first_name'] . " " . $getchildrenrid['surname'];
        $query = mysql_query("INSERT INTO beneficiaries (quoteid, uid, name, percent) VALUES('$quoteid', '$uid', '$nm', '10')");

    $query = mysql_query("SELECT * FROM beneficiaries WHERE quoteid = '$quoteid'");
        $getlastchild = mysql_fetch_assoc($query);

            header("Location: ".$conf['location']."/distribution.php?edit=".$getlastchild['id']);
}

'儿童'专栏:

- id
- **quoteid**
- **uid**
- *first_name*
- middle_names
- *surname*
- dob
- guardian_first_name
- guardian_middlenames
- guardian_nickname
- guardian_surname
- guardian_housenum
- guardian_city
- guardian_street
- guardian_postcode

'受益人'栏目

- id
- **quoteid**
- **uid**
- *name*
- **percent**

需要复制的内容:

  • quoteid INTO quoteid
  • uid INTO uid
  • first_name + last_name INTO名称
  • 并将百分比设置为“10”

此外,这应该在此之后完成:SELECT * FROM children WHERE quoteid ='$ quoteid'已完成。

$ quoteid返回当前报价的唯一报价。

4 个答案:

答案 0 :(得分:1)

假设children具有相同的列名...

INSERT INTO beneficiaries (quoteid, uid, name, percent)
SELECT quoteid, uid, name, percent FROM children WHERE quoteid = '$quoteid'

答案 1 :(得分:1)

试试这个:

INSERT INTO beneficiaries (quoteid, uid, name, percent) select
quoteid, uid, name, percent from children WHERE quoteid = '$quoteid';

答案 2 :(得分:0)

INSERT INTO beneficiaries (quoteid, uid, name, percent) SELECT quoteid, uid, name, percent FROM children WHERE quoteid = '$quoteid';

根据需要调整列名称。

答案 3 :(得分:0)

INSERT INTO beneficiaries (quoteid, uid, name, percent) SELECT (quoteid, uid, name, percent) FROM children WHERE quoteid = '$quoteid'

该查询将按照您的要求执行,当然,假设列是兼容的。

来源:MySQL Docs: INSERT ... SELECT