Skip to content
Advertisement

Adding a conditioned extra field to a query result

I have these two tables

TABLE 1 ( tournaments_data )

TABLE 2 ( tournaments_subscriptions )

Here, i have to read data of table 1 like this

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

Expected result

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:

Result:

See running example at DB Fiddle.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement