连接表但不显示符合 where 子句的行
Posted
技术标签:
【中文标题】连接表但不显示符合 where 子句的行【英文标题】:joining tables but not showing rows where clause is met 【发布时间】:2012-10-27 21:39:19 【问题描述】:我正在加入 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
我想获取日期不在截止表中的所有行。但它给了我相反的东西!我不知道该怎么办,如果有任何帮助,将不胜感激!
EIDT 添加关税表:
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
【问题讨论】:
请发布您的Tarifas
表样本。
【参考方案1】:
试试这个(已编辑):
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
【讨论】:
这不起作用......它仍然给我所有结果,而不排除有截止日期的房间。感谢您尝试 第一个建议将返回没有截止日期或 any 截止日期超出所选日期间隔的房间。使用子查询的新建议应该按预期工作。以上是关于连接表但不显示符合 where 子句的行的主要内容,如果未能解决你的问题,请参考以下文章