MySQL的工作日/周末计数 - 第二部分(MySQL Weekday/Weekend count

2019-09-21 03:21发布

我以前已经张贴了关于这一点,这有助于给我下面的SQL:

 SELECT fname, MONTH( eventDate ) , IF( WEEKDAY( eventDate ) <5, 'weekday', 'weekend' ) AS
 DAY , COUNT( * )
 FROM eventcal AS e
 LEFT JOIN users AS u ON e.primary = u.username
 GROUP BY fname, MONTH( eventDate ) , IF( WEEKDAY( eventDate ) <5, 'weekday', 'weekend' ) ;

这给了我下面的结果:

 fname  MONTH( eventDate )  DAY     COUNT( * )
 Kevin  7                   weekday     3
 Kevin  7                   weekend     1
 Missy  7                   weekday     3
 Missy  7                   weekend     1

我有一些麻烦,试图达到的格式如下:

 fname  MONTH( eventDate )  Weekday COUNT     WEEKEND COUNT
 Kevin   7                   3                  1
 Missy     7                   3                  1

任何人都可以提供一些帮助? 我将不胜感激...

你可以看到我的模式为“用户”和“eventcal”在: MySQL的/ PHP算法工作日/周末数(每月)

Answer 1:

SELECT 
  fname, 
  MONTH(eventDate), 
  SUM(IF(WEEKDAY(eventDate) < 5,1,0)) AS WeekdayCount,
  SUM(IF(WEEKDAY(eventDate) >= 5,1,0)) AS WeekendCount
FROM eventcal AS e
LEFT JOIN users AS u ON e.primary = u.username
GROUP BY fname, MONTH(eventDate);

你想要做你的聚合(在这种情况下SUM)在SELECT和GROUP BY如何希望他们总计(由FNAME,通过一个月)。



文章来源: MySQL Weekday/Weekend count - Part II