简体   繁体   English

SQL 查询 Postgres 12

[英]SQL query Postgres 12

I'm doing an inner join on a table like this:我正在这样的表上进行内部连接:

SELECT * 
FROM  patient p
INNER JOIN 
    vaccine v
ON 
    p.vaccine_id =  v.id 

The condition f.vac_1 = mv.id might not been satisfied in the case where a person have not been vaccinated.在一个人没有接种疫苗的情况下,条件f.vac_1 = mv.id可能不满足。 In such case, I don't want to ignore the row, but instead of displaying the vaccine name (which is the purpose of the inner join) to display an emtpy string.在这种情况下,我不想忽略该行,而是显示一个空字符串,而不是显示疫苗名称(这是内部连接的目的)。

How can this be done?如何才能做到这一点?

Example例子

Table vaccinne表疫苗

id ID name姓名
1 1 Moderna摩德纳
2 2 Comirnaty通讯
3 3 Janssen詹森

Table patient表病人

id ID name姓名 vaccine_id疫苗_id
1 1 john约翰 1 1
2 2 kermit克米特 2 2
3 3 jessica杰西卡

I'm looking for a query to produce:我正在寻找要生成的查询:

id ID name姓名 vaccine_id疫苗_id
1 1 john约翰 Moderna摩德纳
2 2 kermit克米特 Comirnaty通讯
3 3 jessica杰西卡

If I understand correctly, you want a left join starting with foo :如果我理解正确,您需要以foo开头的left join

SELECT * 
FROM foo f LEFT JOIN
     vac v 
     ON f.vac_1 =  mv.id 

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

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