问题
I want to run the following two queries in one:
SELECT id FROM user_settings WHERE ......
$id = id_from_query_above();
$value = 100; // this could be anything
INSERT INTO user_config (sid, value) VALUES($id, $value) ON DUPLICATE KEY UPDATE value=$value
(notice that I want to update if a row associating to the primary key has already been inserted).
回答1:
You want the insert . . . select
syntax:
INSERT INTO user_config(sid, value)
SELECT id, $value
FROM user_settings
WHERE ......
ON DUPLICATE KEY UPDATE value = $value;
来源:https://stackoverflow.com/questions/24816260/mysql-insert-data-into-table-some-data-comes-from-another-table-relational