I have these two tables
TABLE 1 ( tournaments_data )
tournament_id | accound_id | data1 | data2
TABLE 2 ( tournaments_subscriptions )
tournament_id | account_id
Here, i have to read data of table 1 like this
SELECT data1,data2 from tournaments_data WHERE 1
adding an extra field to each row result, which has to be true if a specific account_id is subscribed to a specific tournament, and has to be false if the account_id is not subscribed. A account_id is subscribed to a tournament if we can find a row in the second table where accound_id is coupled to a specific tournament_id. A practical example
tournament_id | account_id | data1 | data2 1 1 data1a data2a 2 5 data1b data2b tournament_id | account_id 1 1
Expected result
data1a data2a true data1b data2b false
querying for account_id=1. Hope it’s clear enough.
Advertisement
Answer
I assumed both columns are named account_id
and there was a typo.
You can use a LEFT JOIN
. For example:
select d.data1, d.data2, case when s.tournament_id is null then 'false' else 'true' end from tournaments_data d left join tournaments_subscriptions s on s.tournament_id = d.tournament_id and s.account_id = d.account_id and s.account_id = 1 -- specific account
Result:
data1 data2 case w... ------ ------ --------- data1a data2a true data1b data2b false
See running example at DB Fiddle.