使用选择插入多行 [英] Insert multiple rows using select

查看:101
本文介绍了使用选择插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一张表中插入2行.第一个将输入来自选择的数据,第二个将使用vars作为数据.我能够插入第一行,但无法插入多行.

I am trying to insert 2 rows into the same table. The first will input data from a select, the second will use vars for data. I am able to insert the first row but having trouble inserting multiple rows.

$ partner_id是将行彼此链接.为此,我在php中使用了生成的32char值.无论如何,是否将mysql设置为edit_partner_id作为插入的第一行的ID,或者由于必须先创建第一行才能获取最后的ID,这是否不可能?

The $partner_id is to link the rows to each other. For this im using a generated 32char value in php. Is there anyway to set the edit_partner_id with mysql as the id of the first row inserted or is this not possible due to the first row has to be created before you can get the last id?

是否还可以为此添加更新,还是必须在单独的查询中运行此更新?

Is it possible to also add an update to this or would I have to run this in a seperate query?

$sql = "INSERT INTO edits_customers (customer_id, creator_id, firstname, surname,
            house_no, address_1, address_2, address_3, city, county, postcode,
            country, email, home_tel, mobile_tel, work_tel, notes, edit_type,
            edit_partner_id )
        (SELECT *, 'before', '{$partner_id}' FROM customers WHERE customers.id = 123),
        ('{$var1}', '{$var2}', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
            '', 'after', $partner_id)";

谢谢

推荐答案

如果我正确理解了您的问题,即您试图在其中插入其他表中的某些数据以及您自己提供的某些数据,那么您应该能够使用UNION做类似的事情:

If I'm understanding your question correctly, where you're trying to insert some data from another table and some data you provide yourself, you should be able to do something like this using UNION:

INSERT INTO SomeTable ( Col1, Col2, Col3 )
SELECT Val1, Val2, Val3 FROM SomeOtherTable
UNION
SELECT 'MyProvidedVal1', 'MyProvidedVal2', 'MyProvidedVal3'

希望有帮助...

这篇关于使用选择插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆