MySQL匹配where子句,带有可选的NULL

时间:2017-09-13 11:39:11

标签: mysql

我有2张桌子 - 病人和发行人。 我用来从患者和发行人表中提取整个患者表以及issuer_name。可选地,可能没有患者标识符的发布者。

如果我这样做: 如果对应的患者表行select * from patients, issuer_name where patients.issuer_of_patient_identifier=issuer.issuer_id为NULL,则issuer_of_patient_identifier不会返回任何内容。

我如何做到这一点?

mysql> describe patients;
+------------------------------+--------------+------+-----+-------------------+-----------------------------+
| Field                        | Type         | Null | Key | Default           | Extra                       |
+------------------------------+--------------+------+-----+-------------------+-----------------------------+
| patient_id                   | int(11)      | NO   | PRI | NULL              | auto_increment              |
| patient_identifier           | varchar(64)  | YES  | MUL | NULL              |                             |
| issuer_of_patient_identifier | int(11)      | YES  | MUL | NULL              |                             |
| medical_record_locator       | varchar(64)  | YES  |     | NULL              |                             |
| patient_name                 | varchar(128) | NO   | MUL | NULL              |                             |
| birth_date                   | datetime     | YES  |     | NULL              |                             |
| deceased_date                | datetime     | YES  |     | NULL              |                             |
| gender                       | varchar(16)  | YES  |     | NULL              |                             |
| ethnicity                    | varchar(45)  | YES  |     | NULL              |                             |
| date_created                 | datetime     | NO   |     | CURRENT_TIMESTAMP |                             |
| last_update_date             | datetime     | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
| last_updated_by              | varchar(128) | NO   |     | NULL              |                             |
+------------------------------+--------------+------+-----+-------------------+-----------------------------+
12 rows in set (0.00 sec)

mysql> describe issuers;
+------------------+--------------+------+-----+-------------------+-----------------------------+
| Field            | Type         | Null | Key | Default           | Extra                       |
+------------------+--------------+------+-----+-------------------+-----------------------------+
| issuer_id        | int(11)      | NO   | PRI | NULL              | auto_increment              |
| issuer_name      | varchar(64)  | NO   | MUL | NULL              |                             |
| issuer_uid       | varchar(128) | YES  |     | NULL              |                             |
| issuer_uid_type  | varchar(64)  | YES  |     | NULL              |                             |
| date_created     | datetime     | NO   |     | CURRENT_TIMESTAMP |                             |
| last_update_date | datetime     | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
| last_updated_by  | varchar(128) | NO   |     | NULL              |                             |
+------------------+--------------+------+-----+-------------------+-----------------------------+

2 个答案:

答案 0 :(得分:1)

查询

select * from patients 
LEFT JOIN issuer_name ON 
patients.issuer_of_patient_identifier = issuer.issuer_id

没有加入:

select * from patients, issuer_name
where patients.issuer_of_patient_identifier = issuer.issuer_id
OR patients.issuer_of_patient_identifier IS NULL

答案 1 :(得分:1)

从不FROM子句中使用逗号。 始终使用正确的,明确的JOIN语法。避免使用逗号的教程。挑战教师展示他们的例子。

您需要LEFT JOIN

select *
from patients p left join
     issuer_name i
     on p.issuer_of_patient_identifier = i.issuer_id;