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.
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.
3
Check if this solves your problem
select * from TABELA order by lg_cliente DESC LIMIT 3
Browser other questions tagged mysql sql mysqli select-sql
You are not signed in. Login or sign up in order to post.
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
– Bia
try like this select * from TABLE order by lg_client DESC LIMIT 3
– Antonio Raichaski
Same error: Mysql server version for the right syntax to use near '3 * from tb_indication
– Bia
You can send me the command you are putting in sql
– Antonio Raichaski
select TOP 3 * from tb_indication order by lg_client DESC
– Bia
try how I put above select * from TABLE order by lg_client DESC LIMIT 3
– Antonio Raichaski
This way he looks for the first 3 where lg_client = 1, and not necessarily the 3 that has more lg_client = 1
– Bia
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
– Antonio Raichaski
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.
– Bia
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.
– Antonio Raichaski
Let’s go continue this discussion in chat.
– Antonio Raichaski
The id repeats yes, because in this table tb_indication, the same user can indicate a client several times
– Bia
select * from TABLE group by id_user order by SUM(lg_client) DESC LIMIT 3. Solved the problem.
– Antonio Raichaski