SQL Select return only one of several

0

I have two tables.

"User" table that has 5 rows and 5 users.

andthe"lease" table that has 20 records, using 5 foreign key ID of the user table

WhenIusethisselectIdid

selectu.nomefromusuarioujoinlocacaolonl.UsuarioId=u.id

Itreturnsmethefollowingvalues...

My question is, how do I return only one value of each?

return like this.

  • Paul
  • Wagner
  • Neto
  • Luke
  • Alan

Without repeating the same name several times?

    
asked by anonymous 11.10.2018 / 10:02

2 answers

2

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

SELECT DISTINCT u.nome FROM usuario u JOIN locacao l ON l.UsuarioId = u.id 
    
11.10.2018 / 10:24
1

Welcome to stackoverflow!

To solve your problem let's use the DISTINCT clause that is used to return only DIFFERENT values, ie no reps.

In your case, just add the term DISTINCT after the SELECT, with:

SELECT DISTINCT ...

11.10.2018 / 13:22