且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL的/ PHP的算法平日/周末计数(每月)

更新时间:2022-11-13 15:35:27

您应该能够做到......(假设你有一个日期表,用户表,和一个链接表)

You should be able to do.... (Assuming you have a date table, a user table, and a link table)

SELECT MONTH(eventDate), DAYOFWEEK(eventDate), 
COUNT(*) 
FROM eventcal as e 
LEFT JOIN users as u ON e.primary = u.username 
GROUP BY MONTH(eventDate), DAYOFWEEK(eventDate);

这应该返回行的负载,3 colums各月产量可达7行。一个是天指数(1 =星期日,7 =星期六),一个是数字月和1是附连到当天的用户数。

That should return a load of rows, 3 colums each up to 7 rows per month. One is the day index (1 = sunday, 7 = saturday), one is the numeric month and one is the number of users attached to that day.

如果您需要限制某些个月,你可以添加一个WHERE子句以及像...

If you need to restrict to certain months, you could add a where clause as well like ...

WHERE eventDate BETWEEN '20090501' AND '20091001'

您还可以使用WITH汇总关键字组后有MySQL的返回月份的周或总计每天为好。但是,这往往是更多的麻烦比它节省了你使用,你必须编写逻辑来判断当前行是一个总的或没有。

You can also use the WITH ROLLUPS keyword after the group by to have mysql return the totals per day of week or per month as well. But this is often more hassle to use than it saves you, as you have to write logic to determine if the current row is a total or not.

编辑:

如果您想直接获取周末/工作日值:

If you want to get the weekend/weekday values directly:

SELECT 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 MONTH(eventDate), IF(WEEKDAY(eventDate)<5, 'weekday', 'weekend');

这将如上回来,但每月只有两行,一为工作日,一为周末。

This will return as above but only 2 rows per month, one for weekdays, one for weekends.

(虽然我不完全以确保您可以按这样的计算值,我觉得你可以 - !让我知道,如果你尝试)

(Although i'm not totaly sure that you can group by a calculated value like this, I think you can - let me know if you try!)