mysql: select all items from table A if not exist in table B

后端 未结 2 1864
梦如初夏
梦如初夏 2020-12-17 15:53

I am having problem with selecting values from table a (id, room_name) where there are no corresponding events in table b (room_id, room_start, room_finish)

my query

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 16:51

    You are missing to use only the events from that room. That is done by matching the id.

    SELECT id, room_name FROM rooms r
    WHERE NOT EXISTS 
    (SELECT * FROM room_events re
          WHERE r.id = re.room_id AND
          room_start BETWEEN '1294727400' AND '1294729200' 
          OR 
          room_finish BETWEEN '1294727400' AND '1294729200')
    

提交回复
热议问题