将WHERE id IN
与条件子查询一起使用时,为什么子查询必须返回1行?为什么此查询适用于limit 1
,但不是没有?我怎样才能做到这一点?
SQLFiddle:http://sqlfiddle.com/#!2/638d30/1
/* remove comment around limit 1 and this query works */
SELECT * FROM ForgeRock WHERE `id` IN (
if(exists (SELECT `fid` FROM `AvailableRock` WHERE `fid` = 2),
(SELECT `fid` FROM `AvailableRock` WHERE `fid` = 2),
(SELECT `id` FROM ForgeRock /* LIMIT 1 */)
)
);
答案 0 :(得分:2)
这有效:
select * from ForgeRock where
(exists(SELECT `fid` FROM `AvailableRock` where `fid` = 1) AND
`id` IN (SELECT `fid` FROM `AvailableRock` where `fid` = 1))
OR NOT exists(SELECT `fid` FROM `AvailableRock` where `fid` = 1)