如何从其他 table - mysql 中正确总结为 "Sickness time"
How to sumarize correctly as "Sickness time" from other table - mysql
我在 mysql 数据库中创建了 "Sickness" table:
+--------+---------+---------+-------------+---------+-------------------------------+
| Id_SICK|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE |
+--------+---------+---------+---------+------------+--------------------+-----------+
| 4 | 26 | ANDREW | WORM |2019-03-19 07:00:00 |2019-03-19 15:00:00 |
+--------+---------+---------+----------------------+--------------------+-----------+
| 5 | 25 | ADAM | GAX |2019-03-21 07:00:00 |2019-03-21 15:00:00 |
+--------+---------+---------+----------------------+--------------------------------+
"Workers" table:
+--------+---------+---------+--
|ID_WORKER | FNAME | LNAME |
+----------+---------+----------
| 25 | ADAM | GAX |
+----------+---------+----------
| 26 | ANDREW | WORM |
+----------+---------+----------
"Orders" table:
+--------+---------+---------+------------+
|ID_ORDER | DESC_ORDER | NUMBER_ORDER |
+----------+---------+--------------------+
| 20 | TEST | TEST |
+----------+---------+--------------------+
和"Order_status" table:
+--------+---------+---------+---------+-------------+--------+----------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE | END_DATE | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 47 | 25 | 20 |2019-03-18 06:50:35 |2019-03-18 15:21:32| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 48 | 25 | 20 |2019-03-20 06:44:12 |2019-03-20 15:11:23| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 50 | 25 | 20 |2019-03-22 06:50:20 |2019-03-22 12:22:33| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 51 | 26 | 20 |2019-03-18 06:45:11 |2019-03-18 15:14:45| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 52 | 26 | 20 |2019-03-20 06:50:22 |2019-03-20 15:10:32| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 53 | 25 | 20 |2019-03-22 06:54:11 |2019-03-22 11:23:45| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
我想根据订单汇总 "total time" 的其他工人(在 order_status table 中),包括从疾病 table 汇总 "sickness time" .我正确地选择了工人(LNAME、FNAME)订单(DESC_ORDER 和 NUMBER_ORDER)和 "TOTAL TIME" 订单。但我无法总结病假时间。我在下面写了 mysql 命令:
SELECT workers.FNAME, workers.LNAME, orders.NUMBER_ORDER, orders.DESC_ORDER, SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))) AS 'TOTAL TIME', SEC_TO_TIME(SUM(TIME_TO_SEC(sickness.END_DATE) - TIME_TO_SEC(sickness.BEGIN_DATE))) AS 'SICKNESS TIME' FROM order_status INNER JOIN workers ON workers.ID_WORKER = order_status.ID_WORKER INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER INNER JOIN sickness ON sickness.ID_WORKER = workers.ID_WORKER WHERE orders.NUMBER_ORDER LIKE 'TEST' GROUP BY workers.ID_WORKER
然后我得到了那个结果:
+--------+---------+---------+-------+------------+------------+-------------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | TOTAL TIME | SICKNESS_TIME|
+----------+---------+---------------+------------+------------+-------------+
| ADAM | GAX | TEST | TEST | 22:25:38 | 24:00:00 |
+----------+---------+---------------+------------+------------+-------------+
| ANDREW | WORM | TEST | TEST | 22:52:12 | 24:00:00 |
+----------+---------+---------------+------------+------------+-------------+
至于"Sickness time is incorrect"因为从"Sickness"分组后ID_SICK就是
+--------+---------+-----+
| Id_SICK| SICKNESS TIME |
+--------+---------+-----+
| 4 | 08:00:00 |
+--------+---------+-----+
| 5 | 08:00:00 |
+--------+---------+-----+
我也总结了 "TOTAL TIME + SICKNESS TIME" 例如
TOTAL TIME: 22:25:38
SICKNESS TIME: 8:00:00
TOTAL + SICKNESS TIME : 22;25:38 + 8:00:00 = 30:25:38
谁能帮我看看怎么处理?我应该写什么样的 mysql 查询?有任何想法吗?感谢您的帮助:)
这实际上是意料之中的。上面的查询是加入 4 tables:
- order_status
- 生病
- 工人
- 订单
"workers" table 与 "order_status 有 1:N 关系]”。
还有“workers”和疾病有1:N关系,其实就算是[=47=也没有关系]1:1.
查询在上述 table 和 重复列值之间创建笛卡尔积 来自每个 table 的重复列值 可以在以上查询。
删除 group by
和 sum
以查看结果集。
例如,在您 id_worker 25 的示例中,您有 3 order_status 行与 1 个疾病行相连(此行值将重复 3 次),与 1 个工人行相连(他们再次将重复 3 次),加入 1 个订单行(相同)。
所以聚合函数sum
合并了重复值。
仅当结果集包含用于聚合函数的所有列的唯一行时才有效
为了解决这个问题,使用子查询聚合结果:
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
workers.id_worker,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'TOTAL TIME',
SEC_TO_TIME(SUM(sicknessAgg.stime)) AS 'SICKNESS TIME'
FROM workers
INNER JOIN (
SELECT sickness.id_worker, SUM((Time_to_sec(sickness.end_date) -
Time_to_sec(sickness.begin_date))) AS stime
FROM sickness
GROUP BY sickness.id_worker
) sicknessAgg
ON sicknessAgg.id_worker = workers.id_worker
INNER JOIN (
SELECT order_status.id_worker, orders.number_order, orders.desc_order, SUM((Time_to_sec(order_status.end_date) -
Time_to_sec(order_status.begin_date))) AS stime
FROM order_status
INNER JOIN orders
ON orders.id_order = order_status.id_order
GROUP BY order_status.id_worker
) order_statusAgg
ON workers.id_worker = order_statusAgg.id_worker
WHERE order_statusAgg.number_order LIKE 'TEST'
GROUP BY workers.id_worker
请注意,在这种情况下,需要从 workers table 开始,因为您要从 order_status
聚合不同数量的行] 和 sickness
tables.
参考文献:
MySQL JOIN with multiple tables and SUMS
我在 mysql 数据库中创建了 "Sickness" table:
+--------+---------+---------+-------------+---------+-------------------------------+
| Id_SICK|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE |
+--------+---------+---------+---------+------------+--------------------+-----------+
| 4 | 26 | ANDREW | WORM |2019-03-19 07:00:00 |2019-03-19 15:00:00 |
+--------+---------+---------+----------------------+--------------------+-----------+
| 5 | 25 | ADAM | GAX |2019-03-21 07:00:00 |2019-03-21 15:00:00 |
+--------+---------+---------+----------------------+--------------------------------+
"Workers" table:
+--------+---------+---------+--
|ID_WORKER | FNAME | LNAME |
+----------+---------+----------
| 25 | ADAM | GAX |
+----------+---------+----------
| 26 | ANDREW | WORM |
+----------+---------+----------
"Orders" table:
+--------+---------+---------+------------+
|ID_ORDER | DESC_ORDER | NUMBER_ORDER |
+----------+---------+--------------------+
| 20 | TEST | TEST |
+----------+---------+--------------------+
和"Order_status" table:
+--------+---------+---------+---------+-------------+--------+----------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE | END_DATE | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 47 | 25 | 20 |2019-03-18 06:50:35 |2019-03-18 15:21:32| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 48 | 25 | 20 |2019-03-20 06:44:12 |2019-03-20 15:11:23| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 50 | 25 | 20 |2019-03-22 06:50:20 |2019-03-22 12:22:33| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 51 | 26 | 20 |2019-03-18 06:45:11 |2019-03-18 15:14:45| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 52 | 26 | 20 |2019-03-20 06:50:22 |2019-03-20 15:10:32| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 53 | 25 | 20 |2019-03-22 06:54:11 |2019-03-22 11:23:45| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
我想根据订单汇总 "total time" 的其他工人(在 order_status table 中),包括从疾病 table 汇总 "sickness time" .我正确地选择了工人(LNAME、FNAME)订单(DESC_ORDER 和 NUMBER_ORDER)和 "TOTAL TIME" 订单。但我无法总结病假时间。我在下面写了 mysql 命令:
SELECT workers.FNAME, workers.LNAME, orders.NUMBER_ORDER, orders.DESC_ORDER, SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))) AS 'TOTAL TIME', SEC_TO_TIME(SUM(TIME_TO_SEC(sickness.END_DATE) - TIME_TO_SEC(sickness.BEGIN_DATE))) AS 'SICKNESS TIME' FROM order_status INNER JOIN workers ON workers.ID_WORKER = order_status.ID_WORKER INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER INNER JOIN sickness ON sickness.ID_WORKER = workers.ID_WORKER WHERE orders.NUMBER_ORDER LIKE 'TEST' GROUP BY workers.ID_WORKER
然后我得到了那个结果:
+--------+---------+---------+-------+------------+------------+-------------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | TOTAL TIME | SICKNESS_TIME|
+----------+---------+---------------+------------+------------+-------------+
| ADAM | GAX | TEST | TEST | 22:25:38 | 24:00:00 |
+----------+---------+---------------+------------+------------+-------------+
| ANDREW | WORM | TEST | TEST | 22:52:12 | 24:00:00 |
+----------+---------+---------------+------------+------------+-------------+
至于"Sickness time is incorrect"因为从"Sickness"分组后ID_SICK就是
+--------+---------+-----+
| Id_SICK| SICKNESS TIME |
+--------+---------+-----+
| 4 | 08:00:00 |
+--------+---------+-----+
| 5 | 08:00:00 |
+--------+---------+-----+
我也总结了 "TOTAL TIME + SICKNESS TIME" 例如
TOTAL TIME: 22:25:38
SICKNESS TIME: 8:00:00
TOTAL + SICKNESS TIME : 22;25:38 + 8:00:00 = 30:25:38
谁能帮我看看怎么处理?我应该写什么样的 mysql 查询?有任何想法吗?感谢您的帮助:)
这实际上是意料之中的。上面的查询是加入 4 tables:
- order_status
- 生病
- 工人
- 订单
"workers" table 与 "order_status 有 1:N 关系]”。
还有“workers”和疾病有1:N关系,其实就算是[=47=也没有关系]1:1.
查询在上述 table 和 重复列值之间创建笛卡尔积 来自每个 table 的重复列值 可以在以上查询。
删除 group by
和 sum
以查看结果集。
例如,在您 id_worker 25 的示例中,您有 3 order_status 行与 1 个疾病行相连(此行值将重复 3 次),与 1 个工人行相连(他们再次将重复 3 次),加入 1 个订单行(相同)。
所以聚合函数sum
合并了重复值。
仅当结果集包含用于聚合函数的所有列的唯一行时才有效
为了解决这个问题,使用子查询聚合结果:
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
workers.id_worker,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'TOTAL TIME',
SEC_TO_TIME(SUM(sicknessAgg.stime)) AS 'SICKNESS TIME'
FROM workers
INNER JOIN (
SELECT sickness.id_worker, SUM((Time_to_sec(sickness.end_date) -
Time_to_sec(sickness.begin_date))) AS stime
FROM sickness
GROUP BY sickness.id_worker
) sicknessAgg
ON sicknessAgg.id_worker = workers.id_worker
INNER JOIN (
SELECT order_status.id_worker, orders.number_order, orders.desc_order, SUM((Time_to_sec(order_status.end_date) -
Time_to_sec(order_status.begin_date))) AS stime
FROM order_status
INNER JOIN orders
ON orders.id_order = order_status.id_order
GROUP BY order_status.id_worker
) order_statusAgg
ON workers.id_worker = order_statusAgg.id_worker
WHERE order_statusAgg.number_order LIKE 'TEST'
GROUP BY workers.id_worker
请注意,在这种情况下,需要从 workers table 开始,因为您要从 order_status
聚合不同数量的行] 和 sickness
tables.
参考文献:
MySQL JOIN with multiple tables and SUMS