左连接不 return 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;
但是我没有在 crew 栏中得到 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
的查询中,您需要继续使用它们。在这种情况下,我认为你想从 crew
开始,因为你是按 FlightNo
聚合的。我不知道您为什么要为不在任何航班上的员工排行。
另一个问题是 group_concat()
忽略了 NULL
值。如果您想查看它们,请使用 coalesce()
:
group_concat(coalesce(e.Name, '') separator ',') as crew,
或者也许:
group_concat(coalesce(e.Name, '<NULL>') separator ',') as crew,
假设我有三个表
剧组
+------------+----------+-------------------------+
| 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;
但是我没有在 crew 栏中得到 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
的查询中,您需要继续使用它们。在这种情况下,我认为你想从 crew
开始,因为你是按 FlightNo
聚合的。我不知道您为什么要为不在任何航班上的员工排行。
另一个问题是 group_concat()
忽略了 NULL
值。如果您想查看它们,请使用 coalesce()
:
group_concat(coalesce(e.Name, '') separator ',') as crew,
或者也许:
group_concat(coalesce(e.Name, '<NULL>') separator ',') as crew,