如何按选定顺序选择所有具有最小开始日期和最大结束日期的工人?

时间:2019-07-15 11:16:00

标签: mysql sql join select

为了“测试”,只有两个工作人员:

-亚当·加克斯(Adam Gax) -安德鲁·沃尔姆(Andrew Worm)

当我编写一个选择SQL查询时:

SELECT MIN(order_status.BEGIN_DATE), MAX(order_status.END_DATE) FROM order_status 
INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER 
WHERE orders.NUMBER_ORDER = 'TEST';

我知道Min的值。 BEGIN_DATE和最高END_DATE在订单“ TEST”中:

  • MIN(order_status.BEGIN_DATE):2019-03-18 06:45:11
  • MAX(order_status.END_DATE):2019-03-22 12:22:33

对于工人Adam Gax:

  • MIN(order_status.BEGIN_DATE):2019-03-18 06:50:35
  • MAX(order_status.END_DATE):2019-03-22 12:22:33

对于工人安德鲁·沃尔姆(Andrew Worm)

  • MIN(order_status.BEGIN_DATE):2019-03-18 06:45:11
  • MAX(order_status.END_DATE):2019-03-22 11:23:45

我尝试了两种方式:

1)使用FULL JOIN-表示LEFT JOINUNION ALLRIGHT JOIN

SELECT MIN(order_status.BEGIN_DATE), MAX(order_status.END_DATE), workers.FNAME, workers.LNAME 
FROM order_status 
LEFT JOIN workers ON workers.ID_WORKER = order_status.ID_WORKER
INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER
WHERE orders.NUMBER_ORDER = 'TEST'
UNION ALL
SELECT MIN(order_status.BEGIN_DATE), MAX(order_status.END_DATE), workers.FNAME, workers.LNAME 
FROM order_status
RIGHT JOIN workers ON workers.ID_WORKER = order_status.ID_WORKER
INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER
WHERE orders.NUMBER_ORDER = 'TEST';

然后我得到了结果,但是它只显示了1个工人:

+----------------------------+--------------------------+--------+-------+
|MIN(order_status.BEGIN_DATE)|MAX(order_status.END_DATE)|  FNAME | LNAME | 
+----------------------------+--------------------------+--------+-------+
| 2019-03-18 06:45:11        |   2019-03-22 12:22:33    | Adam   | Gax   |
+----------------------------+--------------------------+----------+-----+
| 2019-03-18 06:45:11        |   2019-03-22 12:22:33    | Adam   | Gax   |
+----------------------------+--------------------------+----------+-----+

2)我也在尝试FULL OUTER JOIN-LEFT OUTER JOINUNION ALLRIGHT OUTER JOIN

(SELECT MIN(order_status.BEGIN_DATE), MAX(order_status.END_DATE), workers.FNAME, workers.LNAME 
FROM order_status 
LEFT OUTER JOIN workers ON workers.ID_WORKER = order_status.ID_WORKER
INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER
WHERE orders.NUMBER_ORDER = 'TEST') 
UNION ALL 
(SELECT MIN(order_status.BEGIN_DATE), MAX(order_status.END_DATE), workers.FNAME, workers.LNAME 
FROM order_status
RIGHT OUTER JOIN workers ON workers.ID_WORKER = order_status.ID_WORKER
INNER JOIN orders ON orders.ID_ORDER = order_status.ID_ORDER
WHERE zorders.NUMBER_ORDER = 'TEST');

但是与第一个尝试查询的效果相同。我将获得应该达到最小BEGIN_DATE和最大END_DATE的结果:

+----------------------------+--------------------------+--------+-------+
|MIN(order_status.BEGIN_DATE)|MAX(order_status.END_DATE)|  FNAME | LNAME | 
+----------------------------+--------------------------+--------+-------+
| 2019-03-18 06:50:35        |   2019-03-22 12:22:33    | Adam   | Gax   |
+----------------------------+--------------------------+--------+-------+
| 2019-03-18 06:45:11        |   2019-03-22 11:23:45    | Andrew | Worm  |
+----------------------------+--------------------------+--------+-------+ 

有可能为了增加更多的工人,但我想对此案进行简单介绍。

现在我有一个问题:如何按选择的顺序选择所有具有最小begin_date和最大end_date的工人?提前谢谢。

1 个答案:

答案 0 :(得分:1)

我将从表workers开始加入。
然后您需要GROUP BY workers.FNAME, workers.LNAME

SELECT MIN(os.BEGIN_DATE), MAX(os.END_DATE), w.FNAME, w.LNAME 
FROM workers w 
INNER JOIN order_status os ON w.ID_WORKER = os.ID_WORKER
INNER JOIN orders o ON o.ID_ORDER = os.ID_ORDER 
WHERE o.NUMBER_ORDER = 'TEST'
GROUP BY w.FNAME, w.LNAME

由于您在WHERE表中使用了orders子句,因此我认为无需进行LEFT连接。

我也为表使用别名,这是一种常见的良好做法。