开发者

Insert multiple rows using select

开发者 https://www.devze.com 2023-02-17 18:28 出处:网络
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 ro

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.

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 开发者_JAVA百科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)";

Thanks


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'

Hope that helps...

0

精彩评论

暂无评论...
验证码 换一张
取 消