我在连接三个 SQL 表时遇到了问题。简单地认为有 3 个表,分别称为 A、B 和 C。表 A 包含有关出勤的数据,例如类次 ID、日期。另外两个表包含每个类次的数据,例如入场时间,出场时间。一个特定类次的数据只能包含在一个表中。
表A
shift_id | date
--------------------
001 | 2013-12-01
002 | 2013-12-01
003 | 2013-12-01
表B
shift_id | in_time | out_time
------------------------------
001 | 07:10 | 04:10
003 | 07:30 | 05:10
表 C//不包含 out_time 列
shift_id | in_time
--------------------
002 | 07:45
预期输出
shift_id | in_time | out_time
------------------------------
001 | 07:10 | 04:10
002 | 07:45 | 00:00
003 | 07:30 | 05:10
请帮助我。
最佳答案
我认为 UNION
可以帮助你。
SELECT 'FROM A', A.shift_id, B.in_time, B.out_time
FROM A INNER JOIN B ON A.shift_id = B.shift_id
UNION ALL
SELECT 'FROM B', A.shift_id, C.in_time, '00:00'
FROM A INNER JOIN C ON A.shift_id = C.shift_id
关于mysql - SQL连接三张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471866/