我还有另一个给你们。我一直在搜索,但我无法弄清楚这个问题的原因。
基本上,我想对user_id进行DISTINCT,这是一个自然连接的列,存在于表“user”和“leaderboard_entry”中。
这是我的原始查询,效果很好,但我想过滤掉重复项并仅显示第一个最快的用户分数。内部查询基本上抓取100个最近的行,外部查询通过升序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
我尝试过以下操作,即删除“leaderboard_entry。*”,并添加DISTINCT关键字,并明确命名我需要的列:
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
但我得到这个错误,没有任何意义...... :(
#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
任何帮助非常感谢! 猪
答案 0 :(得分:0)
上面的Scrum Meister回答