trembling trembling - 7 months ago 20
SQL Question

join elements from three unrelated tables

SQL newbie here. I have the following schema.



I am to provide a list of all active primary super charged domains belonging to the foobar association.

As far as I know, three tables (sites, domain, and name) cannot be joined together. How can this be done?

Answer

This is an example of joining three tables

SELECT * FROM companies
JOIN sites ON sites.company = companies.id
JOIN domains ON domains.site = sites.id
WHERE 
    sites.is_supercharged = 1 AND sites.is_deleted = 0 AND 
    domains.is_primary = 1 AND domains.is_deleted = 0

It based on the schema (I hope the field names are correct, the text is quite small)

When you joining a table to a query, you need to specify the matching fields reference in the ON part. And then you can add any condition in the WHERE part.