我想选择必须有两个或更多条目的记录,但是应该在表格中包含今天和明天的日期。我以日期格式在表格中保存日期。
SELECT `availibility`.*
FROM (`availibility`)
WHERE `property_id`= 8818
AND (availibility.unavailibility_date between CURDATE()
AND DATE_ADD(CURDATE(),INTERVAL 1 DAY))
我正在使用上面的查询,但即使存在一个日期(今天或明天)也是如此。我想得到这样的记录,应该有两个日期 例如
+---------+----------------+------------+
| ID | property_id | Date |
+---------+----------------+------------+
| 369516 | 8818 | 2013-01-19 |
| 369517 | 8818 | 2013-01-18 |
| 369518 | 8818 | 2013-01-17 |
| 418021 | 8818 | 2013-08-27 |
| 418022 | 8818 | 2013-08-28 |
| 418022 | 8818 | 2013-08-29 |
| 418022 | 2001 | 2013-07-29 |
| 418022 | 2001 |2013-07-30 |
+---------+----------------+------------+
8818属性应该进入记录集,因为这两个日期都存在
答案 0 :(得分:4)
SELECT property_id
FROM yourtable
WHERE date IN (CURDATE(), CURDATE() + INTERVAL 1 DAY)
GROUP BY property_id
HAVING COUNT(DISTINCT date)=2
请参阅小提琴here。
答案 1 :(得分:1)
您可以使用:
NOW() + INTERVAL 1 DAY
如果您只对日期感兴趣,而不是日期和时间,那么您可以使用CURDATE而不是NOW:
CURDATE() + INTERVAL 1 DAY
您的查询应该是
SELECT `availibility`.*
FROM (`availibility`)
WHERE `property_id`= 8818
AND (availibility.unavailibility_date between CURDATE()
AND CURDATE() + INTERVAL 1 DAY // change here
答案 2 :(得分:1)
使用JOIN确保第二条记录存在:
SELECT `availibility`.*
FROM `availibility`
JOIN `availibility` AS availibility_tomorrow ON availibility_tomorrow.property_id = availibility.property_id AND availibility_tomorrow.unavailibility_date = DATE_ADD(CURDATE (),INTERVAL 1 DAY))
WHERE `property_id`= 8818 AND availibility.unavailibility_date = CURDATE()
答案 3 :(得分:0)
如果我理解这个好......
SELECT `property_id`, count(*) as no
FROM (`availibility`)
WHERE `property_id`= 8818
AND (availibility.unavailibility_date=CURDATE()
OR availibility.unavailibility_date=DATE_ADD(CURDATE(),INTERVAL 1 DAY))
having no =2