如何计算请假人数-MySQL

时间:2019-04-08 15:56:15

标签: mysql date count where

大家好,我在mysql基础上都有离开表

console.log(socket.id,currentRoom.id)

“工人表”

+--------+---------+---------+-------------+----------+--------------------------
|ID_LEAVE|ID_WORKER| FNAME   | LNAME | BEGIN_DATE         | END_DATE            | 
+--------+---------+---------+---------+-------------+--------------------+------
| 1      |   4     | LEON    | SPEED |2019-03-20 07:00:00 |2019-03-21 15:00:00  | 
+--------+---------+---------+-------------+----------+--------------------------

“订单”表:

+----------+---------+---------+
|ID_WORKER |  FNAME  | LNAME   |
+----------+---------+----------
| 4        |  LEON   |  SPEED  |
| 6        |  JACK   |  FAR    |
+----------+---------+---------+

“订单状态”表:

+----------+--------------+---------------+
|ID_ORDER  |  DESC_ORDER  | NUMBER_ORDER  |
+----------+--------------+---------------+
| 20       |  TEST        |  TEST         |
+----------+--------------+---------------+

我想做什么?

我想计算在指定日期有多少工人休假,例如,在20-03-2019中只有1个工人休假。我已经尝试过以下查询:

+----------+---------+---------+---------------------+-------------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE          | END_DATE          | 
+----------+---------+---------+----------+------------+---------+--------
| 30       |   4     |    20   |2019-03-18 06:50:35  |2019-03-18 15:21:32|
| 31       |   4     |    20   |2019-03-19 06:44:35  |2019-03-19 15:13:32|
| 32       |   4     |    20   |2019-03-20 06:45:33  |2019-03-20 15:11:23|
| 33       |   4     |    20   |2019-03-21 06:50:01  |2019-03-21 15:20:44|
| 34       |   4     |    20   |2019-03-22 06:50:20  |2019-03-22 12:22:33|
| 35       |   6     |    20   |2019-03-18 06:45:11  |2019-03-18 15:14:45|
| 36       |   6     |    20   |2019-03-19 06:55:32  |2019-03-19 15:33:45|
| 37       |   6     |    20   |2019-03-20 06:50:22  |2019-03-20 15:10:32|
| 38       |   6     |    20   |2019-03-21 06:53:23  |2019-03-21 15:11:44|
| 39       |   6     |    20   |2019-03-22 06:54:11  |2019-03-22 11:23:45|
+----------+---------+---------+------------+---------+------------------+

那我有:

SELECT COUNT(leave.ID_WORKER) AS 'NUMBER OF WORKERS IN LEAVE' 
FROM leave
INNER JOIN order_status
ON order_status.ID_WORKER = leave.ID_WORKER 
INNER JOIN orders
WHERE DATE(order_status.BEGIN_DATE) = '2019-03-20' AND DATE(order_status.END_DATE) ='2019-03-20' AND orders.NUMBER_ORDER = 'TEST'

对于2019-03-21,我得到了:

NUMBER OF WORKERS IN LEAVE = 1

但是对于2019-03-19我有:

 NUMBER OF WORKERS IN LEAVE = 0

为什么会这样,在2019-03-21中,离开的工人数应为1,而在2019-03-19中应为0。我应该在哪里更改它将起作用的方法。谢谢您的任何建议和帮助。

1 个答案:

答案 0 :(得分:0)

您需要做的第一件事是确保您的查询确实返回您想要的结果。让我们先做一些检查。

我在这里提取您的第一个INNER JOIN并更改为SELECT *

SELECT * FROM leave
INNER JOIN order_status
ON order_status.ID_WORKER = leave.ID_WORKER;

这是我执行上面的查询时得到的结果。 Result 1

然后添加第三个联接;

SELECT * FROM leave
INNER JOIN order_status
ON order_status.ID_WORKER = leave.ID_WORKER
INNER JOIN orders;

这将在orders表中添加三列。请参考下面的照片: Result 2

注意:您的第二个INNER JOIN没有ON条件。我建议您通过添加ON order_status.ID_ORDER=orders.ID_ORDER来修复该部分。

到目前为止,所有结果都给出了5行。这5行来自您的order_status表,由于您要使用leave的关系加入ID_WORKER表,因此它将返回与该字段匹配的所有内容。

现在,我们使用第一个WHERE测试您的查询:

SELECT COUNT(leave.ID_WORKER) AS 'NUMBER OF WORKERS IN LEAVE' 
FROM leave
INNER JOIN order_status
ON order_status.ID_WORKER = leave.ID_WORKER 
INNER JOIN orders ON order_status.ID_ORDER=orders.ID_ORDER -- (I've added this condition)
WHERE DATE(order_status.BEGIN_DATE) = '2019-03-20';

WHERE条件在这里不起作用,因为您从BEGIN_DATE表中提取了order_status。如果您查看order_status表,则ID_WORKER=4拥有从2019-03-19到2019-03-21的所有日期的数据。这意味着无论您输入什么日期,都将始终获得NUMBER OF WORKERS IN LEAVE = 1个结果。

我已经使用您的示例数据对您的查询进行了测试,并且在所有日期都得到NUMBER OF WORKERS IN LEAVE = 1。我不确定您是如何在NUMBER OF WORKERS IN LEAVE = 0上获得2019-03-21的。

因此,在完成所有这些操作之后,我设法执行了此查询:

SELECT COUNT(leave.ID_WORKER) AS 'NUMBER OF WORKERS IN LEAVE'
FROM leave
INNER JOIN order_status
ON order_status.ID_WORKER = leave.ID_WORKER 
AND DATE(order_status.BEGIN_DATE) BETWEEN DATE(leave.BEGIN_DATE) AND DATE(leave.END_DATE) -- this condition added
INNER JOIN orders ON order_status.ID_ORDER=orders.ID_ORDER
WHERE DATE(order_status.BEGIN_DATE) = '2019-03-21' 
AND DATE(order_status.END_DATE) ='2019-03-21' 
AND orders.NUMBER_ORDER = 'TEST';

我在查询中所做的就是添加了另一个条件,即您的第一个JOIN。这将返回您想要的结果。 但是,由于数据有限,我不确定上述查询对长期运行是否有用。如果是这样,那将是一件好事。但是,如果没有,请向我反馈您要在查询中添加哪些其他条件。