Select the 3 highest values

Asked

Viewed 4,727 times

3

I have a table that has the following data:

id_indication, id_user,lg_client

A user can indicate multiple clients (lg_client = 1), would like a select that searches for the 3 users that indicated the most, id_user who have more lg_client = 1.

1 answer

3


Check if this solves your problem

select * from TABELA order by lg_cliente DESC LIMIT 3
  • return: error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '3 * from tb_indication order by lg_client DESC LIMIT 0, 25' at line 1

  • try like this select * from TABLE order by lg_client DESC LIMIT 3

  • Same error: Mysql server version for the right syntax to use near '3 * from tb_indication

  • You can send me the command you are putting in sql

  • select TOP 3 * from tb_indication order by lg_client DESC

  • try how I put above select * from TABLE order by lg_client DESC LIMIT 3

  • This way he looks for the first 3 where lg_client = 1, and not necessarily the 3 that has more lg_client = 1

  • 1

    Bia, actually in command he orders in descending order so the first records will always be the largest. Check if your table has records with more than 1 in the lg_client column

  • I understood, but with this sql it returns me the following: id_user: 1,1,34. In other words, it takes the same id twice, I would like to search for the 3 biggest of different users.

  • Are you sure you do not have this user repeated in the database, because this select should not duplicate the information it just shows you have access to.

  • The id repeats yes, because in this table tb_indication, the same user can indicate a client several times

  • 1

    select * from TABLE group by id_user order by SUM(lg_client) DESC LIMIT 3. Solved the problem.

Show 8 more comments

Browser other questions tagged

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