-1

I have this query down there, which makes a left join from tab_usuarios (users), with tab_enderecos (address).

I can have multiple addresses (or none), as you can see on the query result (differs on id_endereco):

id_usuario nome id_endereco logradouro numero cidade uf
1 Administrador 15 âèóã 852 Aparecida SP
1 Administrador 10 teste 852 Aparecida SP
1 Administrador 2 esse 222 cidade SP
2 Novo Usuário
3 Joao Silva 22 3 233546 Lorena SP
3 Joao Silva 21 2 323232 Lorena SP
3 Joao Silva 20 1 323232 Lorena SP
4 fabio 28 Rua Coronel Bráulio Moreira Lima 324354 Lorena SP
5 teste 23 Rua Coronel Bráulio Moreira Lima 45236 Lorena SP
6 Diego 24 Rua Coronel Bráulio Moreira Lima 343434 Lorena SP
7 teste 26 Rua Coronel Bráulio Moreira Lima 122454 Lorena SP

But for what I want, I only want to get 1 address, and I need to be the first entry on the tab_enderecos (sort by id_endereco I guess). For example, for id_usuario = 1, it would return only id_endereco = 2.

I been trying with a couple solutions I found here on Stackoverflow but cant get it to work.

Here is my query:

Select
    u.id_usuario,
    u.nome,
    u.CPF,
    e.cep,
    e.logradouro,
    e.numero,
    e.cidade,
    e.uf,
    e.id_endereco
From
    tab_usuarios u Left Join
    tab_enderecos e On u.id_usuario = e.id_usuario

Thanks.

Barmar
  • 669,327
  • 51
  • 454
  • 560
  • You can try with the following approach: https://www.db-fiddle.com/f/8CowYJGsSjVMVPo5vokLgi/0. Can't get you the full result as you did not provide `tab_usuarios`. Anyways the answer which this question was redirected to does not solve this problem. – lemon May 12 '22 at 15:12

0 Answers0