显示所有记录从表A中跳过表B中的匹配记录

时间:2015-10-27 10:43:00

标签: mysql sql

select r.TableReservationId, d.TableId, d.TableName, d.StoreId
from dining_table d
  LEFT join dining_table_reservation r on d.TableId=r.TableId
where r.Status=0
  and r.StartTime not BETWEEN '2015-10-15 17:40:18' AND '2015-10-15 17:40:18'      

这个只显示匹配记录,但我想要来自dining_table的所有记录,如果记录与条件匹配,则应跳过此记录

2 个答案:

答案 0 :(得分:0)

select r.TableReservationId, d.TableId, d.TableName, d.StoreId
from dining_table d
  LEFT join dining_table_reservation r on d.TableId=r.TableId
  AND r.Status=0
  and r.StartTime not BETWEEN '2015-10-15 17:40:18' AND '2015-10-15 17:40:18'  

LEFT JOIN时,如果将这些右侧表条件放在WHERE子句中,它将作为常规内连接执行。转而使用ON子句!

答案 1 :(得分:0)

所以你想要所有的记录,表格中没有共同的值使用NOT IN和你当前的结果

SELECT * FROM dining_table where TableId NOT IN
(select r.TableReservationId, d.TableId, d.TableName, d.StoreId
from dining_table d
  LEFT join dining_table_reservation r on d.TableId=r.TableId
where r.Status=0
  and r.StartTime not BETWEEN '2015-10-15 17:40:18' AND '2015-10-15 17:40:18');