我有一个像这样的mysql表:
| ID | ID_period | date_start | date_end |
| 1 | 1 | 0000-07-01 | 0000-08-31 |
| 2 | 2 | 0000-09-01 | 0000-10-30 |
| 3 | 3 | 0000-11-01 | 0000-12-28 |
| 4 | 4 | 0000-11-01 | 0000-03-31 |
如何选择此期间0000/07/14 - 0000/08/25之间的ID?
date_start和date_end列是DATE格式。
问题在于,如果我搜索一段时间(包括并交叉),即:从0000-12-12到0000-01-25,我从选择中获得0条记录,我想这一年是' 0000'..我该怎么办呢?
另一个问题是,如果我搜索11-01到12-31这样的时期我得到0结果..因为date_end的最后一天od 12月是28 ..但如果我搜索11-01到12-期间31是因为我想要包含所有记录..所以我想得到id = 3和id = 4的记录
目前我正在使用以下查询:
SELECT ... WHERE '12' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '15' BETWEEN DAY(date_start) and DAY(date_end)
AND '03' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '28' BETWEEN DAY(date_start) and DAY(date_end)
答案 0 :(得分:1)
SELECT *
FROM mytable
WHERE date_start BETWEEN '0000-07-14' AND '0000-08-25'
OR date_end BETWEEN '0000-07-14' AND '0000-08-25'
OR (date_start<'0000-07-14' AND date_end>'0000-08-25')