MySQL: Insert data into table, some data comes from another table (relational)

一曲冷凌霜 提交于 2019-12-12 04:56:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!