2010-04-19 94 views
1

這個問題很容易修復客戶端。但是爲了提高性能,我想直接將其寫入數據庫。在一個查詢中訂購混合型混合mysql搜索結果?

LIST a 
+------+-------+-------+ 
| name | score | cre | 
+------+-------+-------+ 
| Abe | 3  | 1  | 
| Zoe | 5  | 2  | 
| Mye | 1  | 3  | 
| Joe | 3  | 4  | 

想要檢索沒有重複的加入混合結果。

Zoe (1st highest score) 
Joe (1st last submitted) 
Abe (2nd highest score) 
Mye (2nd last submitted) 
... 

客戶端我自己採取每個搜索,並通過它們一步。 但在100.000+以上它變得尷尬。爲了能夠使用LIMIT功能將會使事情變得更加輕鬆!

SELECT name FROM a ORDER BY score DESC, cre DESC; 
SELECT name FROM a ORDER BY cre DESC, score DESC; 
+0

Hello Fredrik,你準確的需要你解釋一下嗎? – Karthik 2010-04-19 11:38:59

回答

1

MySQL不提供分析函數,如ROW_NUMBER

一種方式來實現,這將是兩個查詢不同Order By,連在一起的

Select s.name, s.score_num, c.cre_num 
From 
(
    Select name, @score_num := @score_num + 1 As score_num 
    From your_table, (SELECT @score_num := 0) v 
    Order By score Desc 
) s 
Join 
(
    Select name, @cre_num := @cre_num + 1 As cre_num 
    From your_table, (SELECT @cre_num := 0) v 
    Order By cre Desc 
) c 
On (c.name = s.name) 

回報

name score_num cre_num 
Joe   3  1 
Mye   4  2 
Zoe   1  3 
Abe   2  4 

這是你想要的嗎?

+0

是的,非常好的解決方案!通過加入數據,我可以選擇如何衡量其重要性。最佳的是我的情況實際上不是嚴格的版本,而是總結的版本。做得好! [...] SELECT [...](s.score_num Fredrik 2010-04-19 12:39:16