2013-06-06 112 views
2

說我有一個表稱爲「用戶」,它有40行的記錄,每行有字段:MySQL查詢排序和訂購

id, firstname, group_id, login_count, stay_on_page_count 

組具有

administrator (1), manager (2), employee (3) 

是有可能創建一個查詢,將排序和排序行這種方式

group _id  stay_on_page_count login_count 
    =========  ================== =========== 
    1    100mins    100 
    1    90mins    90 
    2    100mins    100 
    3    100mins    100 

    1    80mins    80 
    1    70mins    70 
    2    90mins    90 
    3    90mins    90 

    1    60mins    60 
    1    50mins    50 
    2    80mins    80 
    3    80mins    80 

    1    40mins    40 
    1    30mins    30 
    2    70mins    70 
    3    70mins    70 

基本上我想創建一個4x4網格視圖使用查詢結果LT。僞代碼可能

SELECT all FROM user table and to group the result in to cluster of 4, 
    while each 4 should have ORDER BY group_id ASC as first priority (1,1,2,3) 
    AND stay_on_page_count ORDER BY DESC as second priority, 
    AND login_count ORDER BY DESC as last or third priority 

我不知道的僞代碼解釋就夠了,但是這是我可以:)

,如果它能夠想出的唯一的事情,然後將它犧牲性能? 有沒有更好的方法來實現這一目標?

我使用MySQL和PHP(CakePHP的2.x的)

感謝

+2

讀取您的示例輸出,我找不出排序條件。你能解釋一下嗎? (僞代碼是受歡迎的) – RandomSeed

+0

你可以在PHP代碼本身做到這一點。據我所知,沒有簡單的解決方案來按列分類結果。 – skparwal

+0

@YaK我已經添加了僞代碼,希望它解釋:) – littlechad

回答

2

一種方法(使用summarised作爲表保持彙總值中的問題列出):

select * from 
(select s1.*, 
     @rank1:[email protected]+1 as rankcalc, 
     floor(@rank1/2) rankgroup, 
     @rank1%2 rankingroup 
from (select @rank1:=1) r1 
cross join 
(select * from summarised where group_id=1 order by stay_on_page_count desc) s1 
union all 
select s2.*, 
     @rank2:[email protected]+1 as rankcalc, 
     @rank2 rankgroup, 
     2 rankingroup 
from (select @rank2:=0) r2 
cross join 
(select * from summarised where group_id=2 order by stay_on_page_count desc) s2 
union all 
select s3.*, 
     @rank3:[email protected]+1 as rankcalc, 
     @rank3 rankgroup, 
     3 rankingroup 
from (select @rank3:=0) r3 
cross join 
(select * from summarised where group_id=3 order by stay_on_page_count desc) s3 
) sq order by rankgroup, rankingroup 

SQLFiddle here

請注意,此解決方案依賴於在子查詢中指定的序列中評估的順序,並且不會被優化器覆蓋 - 這應該適用於當前版本的MySQL,但可能無法在MariaDB中使用(open MySQL的源碼分支)或未來版本的MySQL。

+0

哇!但它的工作。 – RandomSeed

0

雖然這會生成您想要的訂單,但它非常強制和硬編碼。它使得像login_count這樣的假設總是在你列出的範圍內。

SELECT group_ID, Stay_on_Page_count, Login_Count, myset 
FROM (
SELECT group_ID, Stay_on_Page_Count, Login_Count, 1 as mySet 
FROM USERS 
WHERE (GROUP_ID=1 and Login_count >= 90) OR (group_ID in (2,3) and Login_Count=100) 
UNION 
SELECT group_ID, Stay_on_Page_Count, Login_Count, 2 as mySet 
FROM USERS 
WHERE (GROUP_ID=1 and Login_count Between 70 and 80) 
    OR (group_ID in (2,3) and Login_Count=90) 
UNION 
SELECT group_ID, Stay_on_Page_Count, Login_Count, 3 as mySet 
FROM USERS 
WHERE (GROUP_ID=1 and Login_count Between 50 and 60) 
    OR (group_ID in (2,3) and Login_Count=80) 
UNION 
SELECT group_ID, Stay_on_Page_Count, Login_Count, 4 as mySet 
FROM USERS 
WHERE (GROUP_ID=1 and Login_count Between 30 and 40) 
    OR (group_ID in (2,3) and Login_Count=70)) 
ORDER BY myset, group_ID, Login_count Desc