我正在尝试创建一个insert语句,下面是我到目前为止:
INSERT INTO Role (application, developer, role)
VALUES (? ,
(SELECT Id from Developer WHERE firstName='Alice' and lastname='Wonderland'),
'ARCHITECT');
在缺失值字段中,我正在尝试进行连接,以下连接语句返回我需要的值:
SELECT a.id
FROM Application a
JOIN application d ON d.id = a.id and a.category='GAMES'
但是,当我将上一个查询插入到insert语句中的缺失值字段时,它会给出一个错误,指出查询返回的行超过1行(这是故意的,因为我希望返回的所有行都被插入进入角色表)
有人可以就如何解决此问题向我提供反馈意见吗?
答案 0 :(得分:1)
我认为这就是你要找的东西:
INSERT INTO Role(application, developer, role)
SELECT a.id, (SELECT Id from Developer WHERE firstName = 'Alice' and lastname = 'Wonderland'),
'ARCHITECT'
FROM Application a JOIN
application d
ON d.id = a.id and a.category = 'GAMES';
注意:我打算将带有Developer
的子查询移动到FROM
子句中。但是,如果没有匹配项,则会返回NULL
- 这似乎是您的意图。如果我把它放在FROM
子句中,你将得不到匹配(MySQL没有left cross join
)。
答案 1 :(得分:0)
将您的其他参数合并到您的select语句
中INSERT INTO Role (application, developer, role)
SELECT ?, Id, 'ARCHITECT' FROM Application a
JOIN application d ON d.id = a.id and a.category='GAMES'
答案 2 :(得分:0)
您需要使用INSERT INTO .. SELECT FROM
构造来完成此操作。模拟查询看起来如下
INSERT INTO Role (application, developer, role)
select a.id,
dev.Id,
'ARCHITECT'
FROM Application a
JOIN developer dev on a.Id = dev.Id
WHERE a.category='GAMES'
AND dev.firstName='Alice'
and dev.lastname='Wonderland'