简体   繁体   English

按日期分组日期

[英]Group dates by their day of week

I have a problem and can't find a solution. 我有一个问题,无法找到解决方案。

I have a simple table with TV programs, like this: 我有一个简单的电视节目表,如下所示:

CREATE TABLE IF NOT EXISTS programs (
    id_program int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    air datetime, 
    title varchar(50)
);

and some rows in the table: 和表中的一些行:

2003-10-20 17:00    Factor 
2003-10-21 17:00    Factor 
2003-10-22 17:00    Factor
2003-10-20 19:00    Form
2003-10-21 14:00    Factor

How can I obtain the result for Factor, something like this: 如何获得因子的结果,如下所示:

monday - wednesday : 17.00
tuesday : 14.00;

Is it possible to do this in SQL or do I need to fetch data in PHP? 是可以在SQL中执行此操作还是需要在PHP中获取数据?

I think to get exactly what you want in one query is not easily possible. 我认为在一个查询中准确得到你想要的东西并不容易。 But I came to something that is nearly your desired result: 但我找到的东西几乎是你想要的结果:

SELECT TIME(air), title, GROUP_CONCAT(DAYOFWEEK(air)) 
FROM programs WHERE title = 'Factor' 
GROUP BY TIME(air)

This gives me the following result: 这给了我以下结果:

TIME(air)   title   GROUP_CONCAT(DAYOFWEEK(air))
-------------------------------------------------
14:00:00    Factor  3
17:00:00    Factor  2,3,4

With this result you can easily utilize php to get your desired result. 有了这个结果,您可以轻松利用PHP获得所需的结果。 Results like "monday, wednesday, friday-saturday" are possible with this too. 像“星期一,星期三,星期五,星期六”这样的结果也是可能的。

select date_format(`air`,'%d-%b %h:%i') from programs

This doesn't answer the question exactly as this is in MSSQL. 这并不能完全回答MSSQL中的问题。 I know there are MySQL flavors do this though 我知道有MySQL风味可以做到这一点

declare @tempTable TABLE (dt_timeOfProgram datetime, str_name varchar(10));

insert into @tempTable values 
('2003-10-20 17:00', 'Factor'),
('2003-10-21 17:00', 'Factor'),
('2003-10-22 17:00', 'Factor'),
('2003-10-20 19:00', 'Form'),
('2003-10-21 14:00', 'Factor');

select
REPLACE(REPLACE(REPLACE(dow, '</dow><dow>', ','), '<dow>',''),'</dow>', '') as DOW
,   [time]
,   [showName]
from (  select distinct
        CAST(dt_timeOfProgram as time) as [time], 
        str_name [showName],
        (   SELECT DATENAME(dw, t1.dt_timeOfProgram) as [dow]
            FROM @tempTable t1 
            where t1.str_name=t2.str_name
            and  CAST(t1.dt_timeOfProgram as time) = CAST(t2.dt_timeOfProgram as time)
            order by t1.dt_timeOfProgram
            for XML PATH('')) [dow]
    from @tempTable t2) as t

And here is the resultset 这是结果集

Tuesday                     14:00:00.0000000    Factor
Monday,Tuesday,Wednesday    17:00:00.0000000    Factor
Monday                      19:00:00.0000000    Form

This is pretty sloppy work though. 这是非常草率的工作。 I would never do this in a live application. 我永远不会在实时应用程序中这样做。 This logic would be handled by business logic instead of db. 这个逻辑将由业务逻辑而不是db来处理。

Database resources in general are the LEASE scalable. 数据库资源通常是LEASE可伸缩的。 So in the end, you want to use them as little as possible. 所以最后,你想尽可能少地使用它们。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM