Mysql SQL连接三个表

Mysql SQL连接三个表,mysql,sql,Mysql,Sql,我在连接三个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

我在连接三个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\U 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

请帮帮我。

我想工会可以帮你

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

我认为工会可以帮助你

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

假设shift_id是表B和C的外键,即不需要验证表A中是否存在shift_id

SELECT 
    shift_id, in_time, out_time 
FROM 
    B
UNION  
SELECT 
    shift_id, in_time,'00:00' as out_time 
FROM 
    C
否则


如果需要验证表A中是否存在shift_id,则假定shift_id是表B和C的外键,即不需要验证表A中是否存在shift_id

SELECT 
    shift_id, in_time, out_time 
FROM 
    B
UNION  
SELECT 
    shift_id, in_time,'00:00' as out_time 
FROM 
    C
否则

如果需要验证表A中是否存在shift_id,可以使用
IFNULL()
(参考:):

SELECT 
    shift_id, in_time, out_time 
FROM 
    B
UNION  
SELECT 
    shift_id, in_time,'00:00' as out_time 
FROM 
    C
您可以使用
IFNULL()
(参考:):


你写过sql吗?甚至我都不知道如何编写。:-(你写过sql吗?甚至我都不知道如何编写。:-(在第二次选择查询中应该是C.in_时间而不是B.in_时间)在第二次选择查询中应该是C.in_时间而不是B.in_时间