Skip to content
Advertisement

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

Advertisement

Answer

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 
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement