[英]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 ![]() |
name![]() |
---|---|
1 ![]() |
Moderna![]() |
2 ![]() |
Comirnaty![]() |
3 ![]() |
Janssen![]() |
Table patient表病人
id ![]() |
name![]() |
vaccine_id![]() |
---|---|---|
1 ![]() |
john![]() |
1 ![]() |
2 ![]() |
kermit![]() |
2 ![]() |
3 ![]() |
jessica![]() |
I'm looking for a query to produce:我正在寻找要生成的查询:
id ![]() |
name![]() |
vaccine_id![]() |
---|---|---|
1 ![]() |
john![]() |
Moderna![]() |
2 ![]() |
kermit![]() |
Comirnaty![]() |
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.