mysql - How to do a SQL insert statement in two tables with one statement? -
i have 2 tables: message
, message_content
:
message id | customer_id | creation_time | msg_headline | status | order_id message_content message_id | msg | creation_time | employee_id
when insert new data need create record in both tables (linked id
resp. message_id
). possible achieve using single statement?
it not possible insert 2 tables single insert
statement. there few options. 1 of using last_insert_id()[1]
:
insert `message` (`customer_id`, `creation_time`, `msg_headline`, `status`, `order_id`) values (...); insert `message_content` (`message_id`, `msg`, `creation_time`, `employee_id`) values (last_insert_id(), 'message', default, 1)
in actual implementation should switch prepared statement (and php: prepared statements - manual, since mentioned php).
Comments
Post a Comment