0

I want to select the users from a database called log_buyers. log_buyers is a log with all items i sold to users. I use DISTINCT on field USERID to collect only the buyers one time (some people buy more items). I use it like this:

$lsel_log = mysql_query("select DISTINCT userid from log_buyers order by id");
while ($log = mysql_fetch_array($lsel_log)) { 

Now i want to have the 10 most common users and see how much each user in the top paid. In the same table (log_buyers).

  $lsel_log = mysql_query("select DISTINCT userid from log_buyers order by id");
  while ($log = mysql_fetch_array($lsel_log)) {  

$lsel_total_5 = mysql_query("select * from log_buyers where userid = '$log[userid]' order by id desc");
 $total = mysql_num_rows($lsel_total_5);
 $total = $total * 5; 
echo "$log[userid] - $total"; 

The code works fine, except he is doing it for all the users who bought. I only want to do it for the top 10 users. (Most common in log_buyers).

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
Banzouzi
  • 11
  • 5
  • @BernardoLima `TOP` doesn't work in MySQL, that's SQL Server or Oracle. – Niels Keurentjes Aug 26 '14 at 15:27
  • possible duplicate of [Select most common value from a field in MySQL](http://stackoverflow.com/questions/7693613/select-most-common-value-from-a-field-in-mysql) – Robert Aug 26 '14 at 15:32

3 Answers3

0

in the first query, add LIMIT 10 at the end of the query

RichardBernards
  • 3,146
  • 1
  • 22
  • 30
0

It's very easy question you should add LIMIT 10 to your query.

Edit: I think you will find your answer there

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
0

Using GROUP BY with COUNT should get you the most common userids in that table.

SELECT userid FROM log_buyers 
GROUP BY userid 
ORDER BY COUNT(userid) DESC 
LIMIT 10
Don't Panic
  • 41,125
  • 10
  • 61
  • 80