简体   繁体   English

用于工作日/周末计数的MySQL / PHP算法(每月)

[英]MySQL/PHP Algorithm for Weekday/Weekend count (per month)

Before I start coding, I want to come up with a good design first. 在开始编码之前,我想首先想出一个好的设计。 Basically, I have a database table filled with dates, and users who are associated with those dates (the dates are in sql format). 基本上,我有一个填充日期的数据库表,以及与这些日期相关联的用户(日期是sql格式)。

Using PHP I want to, for each user, calculate (count in total) how many weekdays they are associated with, as well as how many weekends they are associated with. 使用PHP我想为每个用户计算(总计)他们与之相关的工作日数,以及他们与之相关的周末数。 I need to have a total count for each month, as well as a grand total. 我需要每个月有一个总数,以及总数。 This needs to to "run" from August until May. 这需要从8月到5月“运行”。

I know that I can determine whether a day is a weekend or a weekday with: 我知道我可以确定一天是周末还是工作日:

 $date = '2007/08/30';
 $weekday = date('l', strtotime($date));

For determining month, I can use SQL, and use a case statement to, for example, get "October" from "10": 为了确定月份,我可以使用SQL,并使用case语句,例如,从“10”获取“October”:

 SELECT MONTH(DATE_SPECIFIED);

What I am most unsure of, though, is what process to go through. 但是,我最不确定的是要经历的过程。 I could easily have ALOT of queries, but that's inefficient. 我可以很容易地进行大量查询,但效率很低。 Ideally, I was thinking about printing the results in an html table. 理想情况下,我正在考虑在html表中打印结果。

Can anyone offer any suggestions/advice on how you might go about it? 任何人都可以就如何进行提供任何建议/意见吗?

Thanks. 谢谢。


EDIT: 编辑:

I have a users table, and an eventcal table. 我有一个用户表和一个eventcal表。

Here is the users table: 这是users表:

 CREATE TABLE `users` (
  `fname` varchar(50) NOT NULL,
  `lname` varchar(50) NOT NULL,
  `role` varchar(75) NOT NULL,
  `region` tinyint(4) unsigned default NULL,
  `username` varchar(25) NOT NULL,
  `password` varchar(75) NOT NULL,
  `new_pass` varchar(5) default NULL,
  PRIMARY KEY  (`username`),
  KEY `role` (`role`),
  KEY `region` (`region`),
  CONSTRAINT `users_ibfk_2` FOREIGN KEY (`region`) REFERENCES `region` (`region`) ON UPDATE CASCADE,
  CONSTRAINT `users_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`role`) ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

And here is the eventcal table: 这是eventcal表:

 CREATE TABLE `eventcal` (
  `id` int(11) NOT NULL auto_increment,
  `region` tinyint(3) unsigned NOT NULL,
  `primary` varchar(25) NOT NULL,
  `secondary` tinyint(1) NOT NULL,
  `eventDate` date NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `primary_2` (`primary`),
  CONSTRAINT `eventcal_ibfk_1` FOREIGN KEY (`primary`) REFERENCES `users` (`username`) ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8

Note that evencal.primary has a foreign key reference to users.username... 请注意,evencal.primary具有对users.username的外键引用...

MySQL has a WEEKDAY() function: MySQL有一个WEEKDAY()函数:

WEEKDAY(date) WEEKDAY(日期)

Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday). 返回日期的工作日索引(0 =星期一,1 =星期二,... 6 =星期日)。

You should be able to use this in a MySQL query with GROUP BY month. 您应该能够在GROUP BY月的MySQL查询中使用它。

My SQL knowledge is very rusty and I'm sure this isn't valid SQL, but you should get the gist from it. 我的SQL知识非常生疏,我确信这不是有效的SQL,但你应该从中得到它的要点。

SELECT user, weekdaysPerMonth, month
FROM
(SELECT COUNT(*) AS weekdaysPerMonth, user, date, month
 FROM table
 WHERE WEEKDAY(date) > 0 AND WEEKDAY(date) < 5)
GROUP BY month

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);

That should return a load of rows, 3 colums each up to 7 rows per month. 这应该返回一行行,3个列,每个行最多7行。 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. 一个是日期索引(1 =星期日,7 =星期六),一个是数字月份,一个是当天附加的用户数量。

If you need to restrict to certain months, you could add a where clause as well like ... 如果你需要限制到某些月份,你可以添加一个where子句,就像...

WHERE eventDate BETWEEN '20090501' AND '20091001'

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. 您还可以在group by之后使用WITH ROLLUPS关键字让mysql返回每周或每月的总计。 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. 但这通常比使用它更麻烦,因为你必须编写逻辑来确定当前行是否为总数。

EDIT: 编辑:

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. 这将返回如上所述,但每月只有2行,一个是工作日,一个是周末。

(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!) (虽然我不能完全确定你可以用这样的计算值进行分组,但我想你可以 - 如果你尝试的话请告诉我!)

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

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