Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate toplist with unique users
    primarykey
    data
    text
    <p>I have a table with the fields <code>id, user_id, condition1, condition2, condition3, score</code>. And each user can have several rows in the table. What I want to do now is to create several toplists. It could be for example a toplist where <code>condition1 = foo</code> and I only want to count each user once but I want the complete best row from each user.</p> <p>So <code>SELECT user_id, MAX(score) AS s FROM table WHERE condition1 = foo ORDER BY s DESC LIMIT 50</code> don't work.</p> <p>There are so many different toplists I want so creating a second table where I store the users best result isn't really an option. Because each user would probably have more then 100 different best results.</p> <p>The two things that are important is that getting the top 50 i quick. But also getting which place a specific user is placed on (which is pretty easy by just checking how many unique users have a bigger score than the specific user).</p> <p><strong>Update:</strong> I tested both Thomas and Quassnoi ideas and Thomas idea took 11 seconds and Quassnoi took 4.5 seconds.</p> <p>Then I figured out another way to do it which is:</p> <pre><code>SELECT ( SELECT id FROM table AS ti WHERE ti.user_id = t.user_id AND condition1 = foo ORDER BY score DESC LIMIT 1 ) FROM table as t WHERE condition1 = foo GROUP BY user_id ORDER BY MAX(score) DESC LIMIT 50 </code></pre> <p>And then I just make another query where a pick out all rows <code>WHERE id IN(all ids returned from the first query)</code> and this way takes 0.4 seconds.</p> <p>Is this a good way of doing it or am I just lucky in my testdata?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload