gpt4 book ai didi

mysql - 插入多个选择

转载 作者:行者123 更新时间:2023-11-29 08:04:16 28 4
gpt4 key购买 nike

我有一个表,我想在其中插入数据,并且值本身必须来自多个表。这必须通过阅读 MySQL 文档按如下方式完成:

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

简单地解释一下,我想将我通过 where 子句所需的航空公司和飞机类型表中的 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

谁有解决办法吗?

最佳答案

由于 airlinecompanyplaneType 之间没有关系,因此您不必执行 JOIN 来执行插入,但由于您正在创建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来完成,因为只返回一个可能的行:

INSERT INTO flight (airlinecompanyId, planetypeId)
SELECT
ac.id,
pt.id
FROM
airlinecompany ac
CROSS JOIN planeType pt
WHERE
ac.naam = 'Brussels Airlines'
AND pt.type = 'Boeing 737'

关于mysql - 插入多个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048388/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com