插入多个选择

时间:2014-04-13 20:59:18

标签: mysql sql

我有一个表,我想在其中插入数据,值本身必须来自多个表。必须通过阅读MySQL文档来完成以下操作:

insert into flight(airlinecompanyId,planetypeId)
select id from airlinecompany where naam = 'Brussels Airlines',
select id from planeType where type = 'Boeing 737';

如此简单的解释,我想通过where clausule将需要的表中的航空公司和平面类型的id插入到航班表列中。

当我尝试此查询时,我不断收到以下错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' select id from planeType where type = 'Boeing 737'' at line 2

有解决方案的人?

1 个答案:

答案 0 :(得分:2)

airlinecompanyplaneType之间没有任何关系,您无需执行JOIN来执行插入操作,但由于您只在{{{{}创建了一行1}},可以使用子选项轻松完成,在flight中包装每个表的SELECT语句

()

也可以使用INSERT INTO flight (airlinecompanyId, planetypeId) SELECT (SELECT id FROM airlinecompany WHERE naam = 'Brussels Airlines'), (SELECT id FROM planeType WHERE type = 'Boeing 737') /* MySQL will permit this with no FROM clause */ 来完成,因为只返回了一个可能的行:

CROSS JOIN