我有一个带有事件列表的数据库表 例如
starteventX = 2019-02-20 endeventX = 2019-03-15
starteventY = 2019-03-11 endeventY = 2019-05-28
我需要知道是否在2019年和04月份参加活动,以便我准备月度报告
我尝试过
$mainlink = mysqli_query($db,"SELECT * FROM eventi WHERE ((year1 >= '$year') OR (year2 <= '$year'))
AND ((month1 >= '$month') OR (month2 <= '$month')) ") or merror($msg = mysqli_error($db));
但无法获取所需的信息
答案 0 :(得分:1)
如果您想要重叠的时间间隔,那就是这样的:
SELECT e.*
FROM eventi e
WHERE e.startevent < '2019-05-01' AND -- starts before the end of the month
e.endevent > '2019-04-01' -- ends after the start of the month
答案 1 :(得分:1)
从概念上讲(假设数据是有效的,即某个事件的结束总是比其开始晚):
SELECT * FROM e
WHERE e.startevent <= '2019-04-30' AND e.endevent >= '2019-04-01'
SELECT * FROM e
WHERE e.startevent <= '2019-04-01' AND e.endevent >= '2019-04-30'