我有SELECT
给我一个特定月份的公寓预订日期:
SELECT db.the_date, db.id_state, db.id_client_booking, bs.class
FROM `day_bookings` as db
LEFT JOIN `bookings_states` as bs ON bs.id= db.id_state
WHERE id_item= 10
AND YEAR(the_date)= 2016
AND MONTH(the_date) = 6
如何将上个月的最后一天添加到此查询中?
我考虑过在第5个月使用相同的查询执行新的LEFT JOIN
,但无法使其工作,语法错误始终存在。
我尝试UNION ALL
有两个相似的选择,只得到了最后一个。
我也尝试过嵌套SELECT
,但它没有通过语法错误。
有关如何将上个月添加到此结果的任何建议吗?
答案 0 :(得分:1)
试试这个:
SELECT db.the_date, db.id_state, db.id_client_booking, bs.class
FROM `day_bookings` as db
LEFT JOIN `bookings_states` as bs ON bs.id= db.id_state
WHERE id_item= 10
AND YEAR(the_date)= 2016
AND (MONTH(the_date) = 6 OR (MONTH(the_date) = 5 AND DAY(the_date) = 31))
;
您需要OR 子句。
答案 1 :(得分:0)
使用
进行管理AND the_date BETWEEN DATE_SUB('2016-06-01', INTERVAL 1 DAY) AND LAST_DAY('2016-06-01')
这种方式适用于每个月,尽管长度为28,30或31个月。
所以最终查询是:
SELECT db.the_date, db.id_state, db.id_client_booking, db.id_item, bs.class
FROM `day_bookings` as db
LEFT JOIN `bookings_states` as bs ON bs.id= db.id_state
WHERE id_item= 10
AND YEAR(the_date)= 2016
AND the_date BETWEEN DATE_SUB('2016-06-01', INTERVAL 1 DAY) AND LAST_DAY('2016-06-01')