如何使用多个连接进行插入?

时间:2016-05-08 00:49:34

标签: mysql

  

类似于this问题,但更高级......

我想取这个结果

SELECT * FROM `orgs`
WHERE `slug` = 'fooA'

这个结果

SELECT * FROM `projects`
WHERE `slug` = 'fooB'

并将其用于此

INSERT INTO `things` 
(`id`, `project`, `org`, `thingA`, `thingB`) 
VALUES 
(NULL, project.id, org.id, 'fooC', 'fooD')

1 个答案:

答案 0 :(得分:2)

INSERT INTO `things` 
(`id`, `project`, `org`, `thingA`, `thingB`) 
SELECT NULL, `project`.`id`, `org`.`id`, 'fooC', 'fooD' FROM `projects`,`orgs`
WHERE `slug` = 'fooA' OR `slug` = 'fooB';