2016-03-28 78 views
0

我在使用INNER JOIN查詢時遇到問題。MYSQL加入條件可能爲空

我有兩個表。我需要部門名稱和所有三個批准者。如果任何審批者都是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) 

我不擅長連接......我在這裏錯過了什麼?

+2

看起來你可以嘗試用'LEFT JOIN'替換所有'INNER JOIN'。這不會消除連接表中具有空值的行。 –

回答

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'或其他值)。