带有连接和多个联合的mySQL查询

时间:2016-11-04 13:36:39

标签: mysql

我有这个查询连接满足条件的两个表:

SELECT * FROM order
JOIN reservations
ON order.reservationsID=reservations.ID
WHERE posname='Fixed'

从这个结果我需要从预订表中过滤到某些日期范围。如果我单独查询预订表(有年,月,块列),我会得到以下查询:

(SELECT * FROM reservations 
WHERE YEAR=2016 AND MONTH=11 AND BLOCK>1)
UNION
(SELECT * FROM reservations 
WHERE YEAR=2016 AND MONTH>11)
UNION
(SELECT * FROM reservations 
WHERE YEAR>2016)

如何将这两个查询合并为一个?任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT * FROM [order]
JOIN reservations ON [order].reservationsID=reservations.ID
WHERE posname='Fixed' AND ( (YEAR=2016 AND MONTH=11 AND BLOCK>1) OR
                            (YEAR=2016 AND MONTH>11) OR
                            (YEAR>2016) )