MYSQL连接条件可能为null

时间:2016-03-28 13:26:42

标签: mysql inner-join

我在使用INNER JOIN时遇到问题。

我有两张桌子。我需要部门名称和所有三个审批人。如果任何批准者为NULL,我也需要显示。

mysql> desc department;
+-------------------+----------+------+-----+---------+----------------+
| Field             | Type     | Null | Key | Default | Extra          |
+-------------------+----------+------+-----+---------+----------------+
| id                | int(8)   | NO   | PRI | NULL    | auto_increment |
| departmentName    | tinytext | YES  |     | NULL    |                |
| primaryApprover   | int(8)   | YES  |     | NULL    |                |
| secondaryApprover | int(8)   | YES  |     | NULL    |                |
| tertiaryApprover  | int(8)   | YES  |     | NULL    |                |
+-------------------+----------+------+-----+---------+----------------+

mysql> desc approver;
+------------------+------------+------+-----+---------+----------------+
| Field            | Type       | Null | Key | Default | Extra          |
+------------------+------------+------+-----+---------+----------------+
| id               | int(8)     | NO   | PRI | NULL    | auto_increment |
| approverName     | tinytext   | YES  |     | NULL    |                |
| approverPosition | tinytext   | YES  |     | NULL    |                |
| approverLogonId  | tinytext   | YES  |     | NULL    |                |
| approverEmail    | tinytext   | YES  |     | NULL    |                |
| isActive         | tinyint(1) | YES  |     | NULL    |                |
+------------------+------------+------+-----+---------+----------------+

以下查询有效,但它不会向我提供主要或次要审批者为NULL的数据:

SELECT 
    a.departmentName as DEPARTMENT, 
    pa.approvername as PRIMARY, 
    sa.approvername as SECONDARY, 
    ta.approvername as TERTIARY 
FROM 
    department as a 
INNER JOIN 
    approver pa on a.primaryapprover=pa.id 
INNER JOIN 
    approver sa on a.secondaryapprover = sa.id 
INNER JOIN 
    approver ta on a.tertiaryapprover = ta.id 
ORDER BY 
    a.departmentname;

使用此查询,我得到了这个结果:

+--------------------------------+---------------------------+---------------------------+------------------------+
| DEPARTMENT                     | PRIMARY_APPROVER          | SECONDARY_APPROVER        | TERTIARY_APPROVER      |
+--------------------------------+---------------------------+---------------------------+------------------------+
| Facilities                     | Washburn, Hoban           | Cobb, Jayne               | Reynolds, Malcomn      |
| Personnel / HR                 | Frye, Kaylee              | Serra, Inara              | Book, Dariel           |
+--------------------------------+---------------------------+---------------------------+------------------------+
2 rows in set (0.00 sec)

但应该得到这个结果:

+--------------------------------+---------------------------+---------------------------+------------------------+
| DEPARTMENT                     | PRIMARY_APPROVER          | SECONDARY_APPROVER        | TERTIARY_APPROVER      |
+--------------------------------+---------------------------+---------------------------+------------------------+
| Business Office                | NULL                      | Rample, Fanty             | Niska, Adelei          |
| Facilities                     | Washburn, Hoban           | Cobb, Jayne               | Reynolds, Malcomn      |
| Personnel / HR                 | Frye, Kaylee              | Serra, Inara              | Book, Dariel           |
| Technical Services             | Tam, River                | NULL                      | Tam, Simon             |
+--------------------------------+---------------------------+---------------------------+------------------------+
4 rows in set (0.00 sec)

我不擅长加入......我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

只需使用LEFT JOINS

SELECT 
    a.departmentName as DEPARTMENT, 
    pa.approvername as PRIMARY, 
    sa.approvername as SECONDARY, 
    ta.approvername as TERTIARY 
FROM 
    department as a 
LEFT JOIN 
    approver pa on a.primaryapprover=pa.id 
LEFT JOIN 
    approver sa on a.secondaryapprover = sa.id 
LEFT JOIN 
    approver ta on a.tertiaryapprover = ta.id 
ORDER BY 
    a.departmentname;

INNER JOIN - 仅保留与两个方匹配的记录。

LEFT JOIN - 保留左表中的所有记录,只保留与右表匹配的记录。

您还可以使用COALESCE将空值替换为默认值,例如' -1'什么的。