我想只选择未来的活动
event s_date start_time end_time
event6 2015-01-30 19:45:00 21:45:00
event5 2015-01-29 20:45:00 21:45:00
event4 2015-01-29 19:45:00 21:45:00
event3 2015-01-28 00:30:00 03:00:00
event2 2015-01-27 09:00:00 11:00:00
event1 2015-01-26 19:45:00 21:45:00
我试过这个,但没有得到正确的结果
SELECT s_date, DATE_FORMAT(s_date, '%W %D %M') AS date, CONCAT(s_date, ' ', end_time) AS dt
FROM schedule
GROUP BY s_date
HAVING dt >= NOW()
答案 0 :(得分:1)
您可以使用TIMESTAMP()
连接日期和时间,然后将其与当前时间进行比较。
例如,要获取尚未开始的事件列表:
SELECT
event, start_time, end_time
FROM
events
WHERE
TIMESTAMP(s_date, start_time) > NOW()
;
请注意,这假设start_time和end_time的类型为TIME
。您应该考虑完全取消s_date
字段并将start_time
和end_time
存储为DATETIME
字段。
请记住,不要使用TIMESTAMP
字段。虽然它们看起来很相似,但TIMESTAMP
会在记录发生变化时更新,而DATETIME
只会存储您明确给出的值。
我不太确定您尝试使用GROUP BY
条款做什么。如果您使用此功能,则每个日期只会为您提供一个事件。由于您未明确设置订单,它甚至不会为您提供该日期的第一个活动。
如果你能澄清你对这一部分的意图,我很乐意尝试改进这个答案。
答案 1 :(得分:0)
我希望我能帮到你:
select * from
(select s_date, max(concat(s_date,' ',start_time)) as dt
from schedule
group by s_date
) as x
where STR_TO_DATE(x.dt, '%Y-%c-%e %r') >= NOW()
我认为 future events 是尚未播出的事件。
答案 2 :(得分:0)
您正在使用group by
,但您没有聚合任何列...因此,在评估'concat()`表达式时获得的值将是随机的。
检查this SQL fiddle是否有解决方案:
MySQL 5.5.32架构设置:
create table schedule(
event varchar(50),
s_date date,
start_time time,
end_time time
);
insert into schedule
values ('event6', '2015-01-30', '19:45:00', '21:45:00')
, ('event5', '2015-01-29', '20:45:00', '21:45:00')
, ('event4', '2015-01-29', '19:45:00', '21:45:00')
, ('event3', '2015-01-28', '00:30:00', '03:00:00')
, ('event2', '2015-01-27', '09:00:00', '11:00:00')
, ('event1', '2015-01-26', '19:45:00', '21:45:00');
查询1 :
select s.*
from schedule as s
where concat(s_date, ' ', end_time) > now()
<强> Results 强>:
| EVENT | S_DATE | START_TIME | END_TIME |
|--------|--------------------------------|--------------------------------|--------------------------------|
| event6 | January, 30 2015 00:00:00+0000 | January, 01 1970 19:45:00+0000 | January, 01 1970 21:45:00+0000 |
| event5 | January, 29 2015 00:00:00+0000 | January, 01 1970 20:45:00+0000 | January, 01 1970 21:45:00+0000 |
| event4 | January, 29 2015 00:00:00+0000 | January, 01 1970 19:45:00+0000 | January, 01 1970 21:45:00+0000 |
| event3 | January, 28 2015 00:00:00+0000 | January, 01 1970 00:30:00+0000 | January, 01 1970 03:00:00+0000 |
查询2 :
-- Little trick:
-- In MySQL you can use HAVING without GROUP BY
-- to filter data using column aliases
select s.*, concat(s_date, ' ', end_time) as e_datetime
from schedule as s
having e_datetime > now()
<强> Results 强>:
| EVENT | S_DATE | START_TIME | END_TIME | E_DATETIME |
|--------|--------------------------------|--------------------------------|--------------------------------|---------------------|
| event6 | January, 30 2015 00:00:00+0000 | January, 01 1970 19:45:00+0000 | January, 01 1970 21:45:00+0000 | 2015-01-30 21:45:00 |
| event5 | January, 29 2015 00:00:00+0000 | January, 01 1970 20:45:00+0000 | January, 01 1970 21:45:00+0000 | 2015-01-29 21:45:00 |
| event4 | January, 29 2015 00:00:00+0000 | January, 01 1970 19:45:00+0000 | January, 01 1970 21:45:00+0000 | 2015-01-29 21:45:00 |
| event3 | January, 28 2015 00:00:00+0000 | January, 01 1970 00:30:00+0000 | January, 01 1970 03:00:00+0000 | 2015-01-28 03:00:00 |
希望这有帮助