我還有一個給你們。我已經搜索過,但我無法弄清楚這個問題的原因。不同於複雜的sql查詢
基本上,我想對user_id做一個DISTINCT,它是一個自然加入並存在於表「user」和「leaderboard_entry」中的列。
這是我的原始查詢,它的效果很好,但我想過濾出重複項,並只顯示第一個最快的用戶分數。內部查詢基本上抓取了最近的100行,而外部查詢通過升序leaderboard_entry_elapsed_time_ms來衡量它們。 「leaderboard_entry *,」
set @t1=0; select * from
(
select @t1 := @t1+1 as leaderboard_entry_youngness_rank, [email protected]/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
我試過以下,這是去除與列的明確命名添加DISTINCT關鍵字一樣,所以我需要:
set @t1=0; select * from ( select @t1 := @t1+1 as leaderboard_entry_youngness_rank, [email protected]/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
任何幫助,非常感謝! 豬
你看過GROUP BY子句嗎? http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html – 2011-01-29 06:13:27