MySQL 加入多个 select 包含视图多个值的语句
MySQL Joining multiple select satement containing multiple values for View
我有一个 Table 有所有的细节
+----------+------------+-----------+
| 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 |
+----------+--------------+---------+
另一个 Table 具有所有 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 |
+----------+--------------+---------+
要求的结果是
Time In, min val and Time Out, 最大值
+----------+------------+-----------+
| 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
但我无法从查询中生成视图...既不加入
试试这个:-
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;
我想这对你有帮助。
我有一个 Table 有所有的细节
+----------+------------+-----------+
| 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 |
+----------+--------------+---------+
另一个 Table 具有所有 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 |
+----------+--------------+---------+
要求的结果是 Time In, min val and Time Out, 最大值
+----------+------------+-----------+
| 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
但我无法从查询中生成视图...既不加入
试试这个:-
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;
我想这对你有帮助。