简体   繁体   中英

Distinct in complex sql query

I have another one for you guys. I've searched around but I can't figure out the cause of this issue.

Basically, I want to do a DISTINCT on user_id, which is a column being naturally joined and exists in the tables "user" and "leaderboard_entry".

This is my original query which works great, but I'd like to filter out duplicates and show only the first fastest user score. The inner query basically grabs 100 of the most recent rows, and the outer query resorts them by ascending leaderboard_entry_elapsed_time_ms.

set @t1=0; select * from 
( 
   select @t1 := @t1+1 as leaderboard_entry_youngness_rank, 1-@t1/100 as 
     leaderboard_entry_youngness_based_on_expiry, 
   leaderboard_entry.*, 
   NOW()-leaderboard_entry_timestamp as leaderboard_entry_age_in_some_units , 
   TO_DAYS(NOW())-TO_DAYS(leaderboard_entry_timestamp) 
     as leaderboard_entry_age_in_days , leaderboard.leaderboard_quiz_mode , 
   leaderboard.leaderboard_load_key , 
   user.user_name 
   from leaderboard_entry 
   natural join
   leaderboard 
   natural join 
   user
   where 
   (leaderboard_load_key = 'es-en-animals-1' 
   or leaderboard_load_key = '-es-en-animals-1' ) 
   and leaderboard_quiz_mode = '0'
   order by leaderboard_entry_age_in_some_units asc , 
     leaderboard_entry_timestamp asc limit 0, 100 
) as outer_temp 
order by 
  leaderboard_entry_elapsed_time_ms asc , 
  leaderboard_entry_timestamp asc 
limit 0, 50

I've tried the following, which is removing "leaderboard_entry.*, " and adding the DISTINCT keyword like so with explicit naming of the columns I need:

set @t1=0; select * from 
( 
   select @t1 := @t1+1 as leaderboard_entry_youngness_rank, 1-@t1/100 as 
     leaderboard_entry_youngness_based_on_expiry, 

NOW()-leaderboard_entry_timestamp as leaderboard_entry_age_in_some_units , TO_DAYS(NOW())-TO_DAYS(leaderboard_entry_timestamp) as leaderboard_entry_age_in_days , leaderboard.leaderboard_quiz_mode , leaderboard.leaderboard_load_key , user.user_name

distinct leaderboard_entry.user_id, leaderboard_entry.leaderboard_entry_id, leaderboard_entry.leaderboard_id, leaderboard_entry.leaderboard_entry_timestamp, leaderboard_entry.leaderboard_entry_elapsed_time_ms,

from leaderboard_entry natural join leaderboard natural join user where (leaderboard_load_key = 'es-en-animals-1' or leaderboard_load_key = '-es-en-animals-1' ) and leaderboard_quiz_mode = '0' order by leaderboard_entry_age_in_some_units asc , leaderboard_entry_timestamp asc limit 0, 100 ) as outer_temp order by leaderboard_entry_elapsed_time_ms asc , leaderboard_entry_timestamp asc limit 0, 50

But i get this error and it makes no sense... :(

#1064 - You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL 
server version for the right syntax to use near 
'distinct leaderboard_entry.user_id, leaderboard_entry.leaderboard_entry_id, ' 
at line 12

Any help much appreciated! swine

如上所述由Scrum Meister回答

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM