SQL Select return only one of several

Asked

Viewed 324 times

1

have two tables.

Table "user" which has 5 lines and 5 users.

inserir a descrição da imagem aqui

is the "location" table that has 20 records, using 5 Foreign key of user table ID

When I use this select I made

select u.nome from usuario u join locacao l on l.UsuarioId = u.id 

it returns me the following values...

inserir a descrição da imagem aqui

My doubt is, how do I make it return only one value each?

return like this.


  • Pablo
  • Wagner
  • Grandson
  • Lucas
  • Alan

without repeating the same name several times?

2 answers

3


To return only one value of each, without repetition, you must use DISTINCT, the query would look like this:

SELECT DISTINCT u.nome FROM usuario u JOIN locacao l ON l.UsuarioId = u.id 

0

Welcome to the stackoverflow!

To solve your problem we will use the clause DISTINCT that is used to return only values DISTINGUISHED, that is, without repetitions.

in your case just add the term DISTINCT after SELECT, getting:

SELECT DISTINCT ... [rest of the scope of your query]

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.