连接表但不显示符合where子句的行

时间:2012-10-27 21:39:19

标签: mysql join between

我正在加入mysql中的表,但我有一个表保存日期的表:

table: cutoff

      date       roomid  
---------------------------  
    2012-11-03     25
    2012-11-04     25
    2012-11-05     25
    2012-11-06     25

这是我的SQL查询:

SELECT t.RoomId, t.USD, t.MXN, i.Image, a.RoomName, a.RoomDesc, cu.date FROM Tarifas AS t 
INNER JOIN Images AS i ON i.RoomId = t.RoomId
INNER JOIN Availability AS a ON a.RoomId = t.RoomId 
INNER JOIN cutoff AS cu ON cu.roomid = t.RoomId AND cu.date NOT BETWEEN '$from' and '$to' 
WHERE t.Start <= '$from' AND t.End >= '$from' ORDER BY USD 

我想得到日期不在截止表中的所有行。但它给了我相反的意义!我不知道该怎么办,非常感谢任何帮助!

添加TARIFA表:

table: tarifas
RoomId      Start         End       USD       MXN
---------------------------------------------------
25        2012-11-01   2012-11-20   60.00    620.00
26        2012-11-01   2012-11-20   80.00    820.00
27        2012-11-01   2012-11-20   55.00    570.00

1 个答案:

答案 0 :(得分:0)

试试这个(已编辑):

SELECT t.RoomId, t.USD, t.MXN, i.Image, a.RoomName, a.RoomDesc
FROM Tarifas AS t 
INNER JOIN Images AS i ON i.RoomId = t.RoomId
INNER JOIN Availability AS a ON a.RoomId = t.RoomId 
WHERE t.RoomID NOT IN
    (SELECT DISTINCT roomid FROM cutoff
    WHERE date BETWEEN '$from' AND '$to')
AND t.Start <= '$from' AND t.End >= '$from'
ORDER BY USD