如何组合此INSERT和SELECT MySQL语句?

时间:2013-12-02 20:41:05

标签: mysql

这会拉回一行:

SELECT id FROM pin WHERE pinpicsid = '1'

我需要将该语句的结果放到这个X所在的位置:

INSERT INTO user_collection (pinid, username)
VALUES (X, 'ethanwa')

有没有办法将这两者结合起来?

2 个答案:

答案 0 :(得分:4)

使用INSERT INTO SELECT -statement

INSERT INTO user_collection (pinid, username)
SELECT id, 'ethanwa' FROM pin WHERE pinpicsid = '1'

答案 1 :(得分:1)

INSERT INTO user_collection (pinid, username)
SELECT id, 'ethanwa' 
FROM pin 
WHERE pinpicsid = '1'