我有一个表,我想在其中插入数据,值本身必须来自多个表。必须通过阅读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
有解决方案的人?
答案 0 :(得分:2)
airlinecompany
和planeType
之间没有任何关系,您无需执行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