我有一张包含所有详细信息的表格
+----------+------------+-----------+
| staff_id | date | time |
+----------+------------+-----------+
| 1 | 2015-02-20 | 07:00 |
| 2 | 2015-02-20 | 07:01 |
| 3 | 2015-02-20 | 07:05 |
| 1 | 2015-02-20 | 07:02 |
| 1 | 2015-02-20 | 07:04 |
+----------+--------------+---------+
另一个包含所有 OUT 详细信息的表
+----------+------------+-----------+
| staff_id | date | time |
+----------+------------+-----------+
| 1 | 2015-02-20 | 13:00 |
| 2 | 2015-02-20 | 13:45 |
| 3 | 2015-02-20 | 13:45 |
| 1 | 2015-02-20 | 13:47 |
| 1 | 2015-02-20 | 13:48 |
+----------+--------------+---------+
需要的结果是什么 进入时间、最小值和超时、最大值
+----------+------------+-----------+
| staff_id | date | time IN | Time Out
+----------+------------+-----------+
| 1 | 2015-02-20 | 07:00 | 13:48
| 2 | 2015-02-20 | 07:01 | 13:45
我在做什么
SELECT *
FROM
(SELECT sai.staff_id AS staff_id_in,
sai.date AS date_in,
sai.time AS time_in,
sai.ip4 AS ip4_in,
sai.location_id AS location_id_in,
'1' AS atd_in
FROM staff_attendance_in sai
ORDER BY staff_id ASC, time ASC) AS sub
GROUP BY staff_id_in,
date_in
UNION
SELECT *
FROM
(SELECT sao.staff_id AS staff_id_out,
sao.date AS date_out,
sao.time AS time_out,
sao.ip4 AS ip4_out,
sao.location_id AS location_id_out,
'2' AS atd_out
FROM staff_attendance_out sao
ORDER BY time DESC) AS sub
GROUP BY staff_id_out,
date_out
但我无法从查询生成 View ...也无法加入
最佳答案
试试这个:-
SELECT I.staff_id, I.date, MIN(I.time_IN), MAX(O.Time Out)
FROM IN I JOIN OUT O
ON I.staff_id = O.staff_id
GROUP BY I.staff_id, I.date;
我认为这可以帮助你。
关于MySQL连接包含多个 View 值的多个选择语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624205/