SQL按当天排序

时间:2014-08-26 16:25:29

标签: php mysql sql

我有一个这样的表,想从现在的日期名开始订购它。

title   date_firstcall      totalViews  Tag
name A  2014-08-25 10:19:49     2   Monday
name B  2014-08-24 16:03:36     3   Sunday
name C  2014-08-24 14:54:47     2   Sunday
name C  2014-08-23 11:06:19     3   Saturday
name A  2014-08-23 00:05:35     16  Saturday
name B  2014-08-22 10:05:53     4   Friday
name A  2014-08-22 00:11:28     25  Friday
name C  2014-08-21 19:28:54     1   Thursday
name A  2014-08-21 08:44:05     13  Thursday
name C  2014-08-20 22:42:49     1   Wednesday
name D  2014-08-19 16:22:42     2   Tuesday
name A  2014-08-19 15:43:57     10  Tuesday
name B  2014-08-19 09:36:52     1   Tuesday

我没有工作的SQL语句:

SELECT title as Seite, DAYNAME(CURRENT_DATE) AS diff,
 sum( if( Tag = 'Monday', totalViews, 0 ) ) AS Montag,
 sum( if( Tag = 'Wednesday', totalViews, 0 ) ) AS Mittwoch,
 sum( if( Tag = 'Tuesday', totalViews, 0 ) ) AS Dienstag,
 sum( if( Tag = 'Thursday', totalViews, 0 ) ) AS Donnerstag,
 sum( if( Tag = 'Friday', totalViews, 0 ) ) AS Freitag,
 sum( if( Tag = 'Saturday', totalViews, 0 ) ) AS Samstag,
 sum( if( Tag = 'Sunday', totalViews, 0 ) ) AS Sonntag
 FROM tmp  GROUP BY title ORDER BY diff

结果表大喊一开始是当天,然后是第二天。 喜欢:

        Seite   current day     next day    Dienstag    Mittwoch Donnerstag     Freitag     Samstag
       name A        2            7           0           3        9               2            2
       name c        3            2           1           0        9               2            2

预先提示任何提示。

1 个答案:

答案 0 :(得分:1)

使用WEEKDAY将星期几作为数字。减去当前模7的当前日期,因此它将环绕,然后转动它。

SELECT title,
       SUM(IF(WEEKDAY(date_firstcall) = WEEKDAY(NOW()), totalViews, 0)) AS current_day,
       SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 1, totalViews, 0)) AS day_plus_1,
       SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 2, totalViews, 0)) AS day_plus_2,
       SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 3, totalViews, 0)) AS day_plus_3,
       SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 4, totalViews, 0)) AS day_plus_4,
       SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 5, totalViews, 0)) AS day_plus_5,
       SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 6, totalViews, 0)) AS day_plus_6
FROM tmp
GROUP BY title

DEMO

以下是如何使用动态SQL获取日期名称:

SET @sql = (SELECT CONCAT('
     SELECT title,
            SUM(IF(WEEKDAY(date_firstcall) = WEEKDAY(NOW()), totalViews, 0)) AS ', DAYNAME(NOW()),
    ',      SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 1, totalViews, 0)) AS ', DAYNAME(DATE_ADD(NOW(), INTERVAL 1 DAY)),
    ',      SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 2, totalViews, 0)) AS ', DAYNAME(DATE_ADD(NOW(), INTERVAL 2 DAY)),
    ',      SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 3, totalViews, 0)) AS ', DAYNAME(DATE_ADD(NOW(), INTERVAL 3 DAY)),
    ',      SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 4, totalViews, 0)) AS ', DAYNAME(DATE_ADD(NOW(), INTERVAL 4 DAY)),
    ',      SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 5, totalViews, 0)) AS ', DAYNAME(DATE_ADD(NOW(), INTERVAL 5 DAY)),
    ',      SUM(IF((WEEKDAY(date_firstcall)-WEEKDAY(NOW()) % 7) = 6, totalViews, 0)) AS ', DAYNAME(DATE_ADD(NOW(), INTERVAL 6 DAY)),
    '       FROM tmp
     GROUP BY title'));
PREPARE stmt FROM @sql;
EXECUTE stmt;

DEMO