how to make an inner join getting the description

1

My tables:

agencias (id, nome_agencia, ads -> FK pra tabela de baixo)
ads (id, nome, agencia -> essa é FK pra tabela anterior)

The problem is this: each agency will have ads no ads have a lead agency

I want to make a select displaying agencia_id, agency_name, ads_id, ads_agency_name

I tried that, but I do not know how to "fit" the agency name.

SELECT agencias.id as id_agencia, agencias.nome as agencia_nome, ads.id as id_ads, ads.agencia as ads_agencia_id
FROM agencias
INNER JOIN ads
ON agencias.id=ads.agencia
    
asked by anonymous 14.12.2015 / 16:08

1 answer

2

You just need to give a nickname to your tables

SELECT 
    ag.id as id_agencia, 
    ag.nome as agencia_nome, 
    ad.id as id_ads, 
    ad.agencia as ads_agencia_id
FROM 
    agencias ag
INNER JOIN ads ad ON ad.id = ag.ads -- ag.ads = FK ads -> agencia
    
14.12.2015 / 16:14