子查询返回超过1行|是否可以使用“AND NOT IN”?

时间:2018-05-05 18:53:42

标签: mysql sql

Control.ForeColor

2 个答案:

答案 0 :(得分:0)

您可以使用NOT IN 并避免使用隐式连接...使用显式连接例如:INNER JOIN

SELECT r.rom_ID
FROM rom r
WHERE r.rom_ID NOT IN (

SELECT r.rom_ID
FROM rom r
INNER JOIN  booking b  ON b.rom_ID = r.rom_ID 
and (
     '2018-05-08' BETWEEN b.ankomstdato AND b.utsjekkdato OR
         '2018-05-12' BETWEEN b.ankomstdato AND b.utsjekkdato OR 
         ('2018-05-08' <= b.ankomstdato AND  '2018-05-12' >= b.utsjekkdato
    ) ) )  AND r.romtype_ID = 2
    LIMIT 1;

答案 1 :(得分:0)

我的猜测是,您想要的查询是在类型2期间的免费房间。这看起来像:

SELECT r.rom_ID
FROM rom r
WHERE NOT EXISTS (SELECT 1
                  FROM booking b
                  WHERE b.utsjekkdato > '2018-05-08' AND
                        b.ankomstdato < '2018-05-12'
                 ) AND
      r.romtype_ID = 2
LIMIT 1;

请注意,严格的不等式可以是<=和/或>=,具体取决于可用的含义。