简体   繁体   中英

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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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