[英]Mysql: Selecting distinct value with 2 conditions
I have a table called jobs with fields: id, status. 我有一个名为Jobs的表,其字段为:id,状态。 And another table called jobs_history with fields: id, jobid, status, added_time.
另一个名为Jobs_history的表具有以下字段:ID,JobID,状态,added_time。
Every time a job is edited through php, I insert the updated data into jobs_history table with jobid, user selected status, and added_time. 每次通过php编辑作业时,我都会将更新的数据插入具有jobid,用户选择的状态和added_time的jobs_history表中。
A job maybe edited many times so there could be many rows in jobs_history table for one job. 一项作业可能被编辑了很多次,因此Jobs_history表中可能有多个行。 And the status field may contains different status like, 1st status would be "Under review", 2nd could be "In progress", 3rd could be "E-mail sent", 4th "Completed".
并且状态字段可能包含其他状态,例如,第一个状态为“正在审核”,第二个状态为“进行中”,第三个状态为“电子邮件已发送”,第四个状态为“已完成”。
Now, I need to count the total number of jobs at a specific datetime that had status "In progress" on that date. 现在,我需要计算在特定日期时间状态为“进行中”的作业总数。 Here is some example data in jobs_history table:
这是Jobs_history表中的一些示例数据:
id | jobid | status | added_time
--------------------------------------
1 | 10 | Under review | 2014-05-20 01:00:00
2 | 10 | In progress | 2014-05-21 02:30:00
3 | 10 | E-mail sent | 2014-05-23 10:00:00
4 | 10 | Completed | 2014-05-23 04:00:00
5 | 11 | Under review | 2014-05-19 05:00:00
6 | 11 | In progress | 2014-05-20 06:00:00
7 | 11 | E-mail sent | 2014-05-20 07:00:00
8 | 11 | Completed | 2014-05-22 08:00:00
9 | 12 | Under review | 2014-05-23 09:00:00
10 | 12 | In progress | 2014-05-24 02:00:00
11 | 13 | Under review | 2014-05-20 10:00:00
12 | 13 | In progress | 2014-05-20 12:00:00
I want to count total number of jobs that had status "In progress" on the 2014-05-21 06:00:00. 我想计算2014-05-21 06:00:00上状态为“进行中”的作业总数。 The query should produce result 2, that is jobid 10 and 13, because those are the only jobs that had last status "In progress" on 2014-05-21 06:00:00.
该查询应产生结果2,即Jobid 10和13,因为这些是唯一在2014-05-21 06:00:00上具有“正在处理”状态的作业。
I have about 400,000 data in the jobs_history table so the query should be written in the fastest way possible. 我在jobs_history表中大约有400,000个数据,因此应以最快的方式编写查询。
Thanks and I very much appreciate your help on this. 谢谢,我非常感谢您的帮助。
If your date format is different you can use date_format function. 如果日期格式不同,则可以使用date_format函数。 Check this link http://sqlfiddle.com/#!2/4a6e8b/17
检查此链接http://sqlfiddle.com/#!2/4a6e8b/17
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.