I have four different tables, one main SPECIAL table containing only id’s that reference to the CONTRACT and PHONE tables.
My query looks as follows:
SELECT *
FROM `specials` specials
INNER JOIN `contract` contracts
ON specials.contract_id = contracts.id
INNER JOIN `phone` phones
ON specials.phone_id = phones.id
INNER JOIN `ugets` ugets
ON specials.id = ugets.special_id
At the moment, this only gets ONE row from the UGETS table, but I need all rows from this specific table with the correct special_id.
Can anyone please point me in the correct direction? I can’t seem to help myself with Googling it.
Your query looks about right, unless you also want all rows, regardless of the existence of a
contractorphone. In this case, you’ll want to useOUTER JOINorLEFT JOINinstead ofINNER JOIN:Fields from tables where there are not records for a given special will be returned with
NULL, but all rows from tables where there’s a match will be shown.Beware this could have adverse performance impacts (possibly to the extent it voids the benefits of getting all four tables in a single query) – you should evaluate the gains vs. costs and see if you still want a single query.