繁体   English   中英

左联接不返回Null值

[英]Left join doesn't return the Null value

假设我有三个桌子

船员

+------------+----------+-------------------------+
| EmployeeID | FlightNo | DepartureTime           |
+------------+----------+-------------------------+
|         2  | AA123    | 2018-12-30 09:30:45     |
|         7  | AA123    | 2018-12-30 09:30:45     |
|         8  | AA123    | 2018-12-30 09:30:45     |

员工类型

+----------------+-----------------+
| EmployeeTypeID | Name            |
+----------------+-----------------+
|              1 | Pilot           |
|              2 | First Officer   |
|              3 | Stewardess      |

和员工

+------------+-----------+----------------+
| EmployeeID | Name      | EmployeeTypeID |
+------------+-----------+----------------+
|          2 | James     |              2 |
|          3 | Alexandra |              3 |
|          4 | Alina     |              2 |
|          6 | Peter     |              2 |
|          7 | NULL      |              3 |
|          8 | John      |              1 |
|          9 | Frank     |              1 |

我已经加入了这三个表,但是对于名称为NULL的EmployeeID 7,我没有使用左连接获得NULL值

select crew.FlightNo, group_concat(distinct(crew.DepartureDateAndTimeUTC) separator ',') as time, group_concat(employee.Name separator ',') as crew,group_concat(distinct(employeetype.Name)separator ',') as type
from employee 
left join crew on employee.EmployeeID = crew.EmployeeID 
inner join employeetype 
on employeetype.EmployeeTypeID = employee.EmployeeTypeID group by crew.FlightNo;

但是我在乘员列中没有得到Null

+----------+---------------------+---------------------------------+----------------------------------+
| FlightNo | time                | crew                            | type                             |
+----------+---------------------+---------------------------------+----------------------------------+
| AA123    | 2018-12-30 09:30:45 | James,John                      | Pilot,First Officer, Stewardess  |

我想要在剧组专栏中James,John,NULL

我认为您打算:

select c.FlightNo,
       group_concat(distinct c.DepartureDateAndTimeUTC) separator ',') as time,
       group_concat(e.Name separator ',') as crew,
       group_concat(distinct et.Name) separator ',') as type
from crew c left join
     employee e
     on e.EmployeeID = c.EmployeeID left join
     employeetype et
     on et.EmployeeTypeID = e.EmployeeTypeID
group by c.FlightNo;

通常在带有left join s的查询中,您需要继续使用它们。 在这种情况下,我认为您想从crew入手,因为您正在按FlightNo进行汇总。 我不知道您为什么要为不在任何航班上的员工排位。

另一个问题是group_concat()忽略NULL值。 如果要查看它们,请使用coalesce()

   group_concat(coalesce(e.Name, '') separator ',') as crew,

也许:

   group_concat(coalesce(e.Name, '<NULL>') separator ',') as crew,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM