简体   繁体   English

条件可能为空的MYSQL连接

[英]MYSQL Joins where conditions may be null

I'm having an issue with a query using INNER JOIN. 我在使用INNER JOIN进行查询时遇到问题。

I have two tables. 我有两张桌子。 I need the department name and all three approvers. 我需要部门名称和所有三个批准者。 If any of the approvers are NULL, I need that displayed also. 如果任何批准者为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    |                |
+------------------+------------+------+-----+---------+----------------+

The following query works, but it does not give me data where the primary or secondary approver are 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;

Using this query, I get this result: 使用此查询,我得到以下结果:

+--------------------------------+---------------------------+---------------------------+------------------------+
| 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)

but should get this result: 但应该得到以下结果:

+--------------------------------+---------------------------+---------------------------+------------------------+
| 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)

I'm not good at joins to begin with....what am I missing here? 我不擅长加入。...我在这里想念什么?

Just use 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 - keeps only records that match from both sides . INNER JOIN -只保留双方的匹配记录。

LEFT JOIN - keeps all the records from the left table, and only the record matching from the right table. LEFT JOIN保留左侧表中的所有记录,仅保留右侧表中的记录。

You can also use COALESCE to replace null values with a default value like '-1' or something. 您也可以使用COALESCE将空值替换为默认值,例如-1或类似的东西。

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

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