2016-05-17 89 views
-1

我想根據與某個特定player_id有最高可能的contract_id的數據進行分組。這聽起來很簡單,但它不像預期的那樣工作。MYSQL Groupby和Orderby查詢

SELECT distinct(f1.player_id), f1.contract_id,f1.feature_id 
from (select p.player_id,p.player_name, p.player_no, CASE c.action 
when 'submit' then 'تقديم' 
when 'approve' then 'موافقة' 
when 'reject' then 'رفض' 
when 'object' then 'اعتراض' 
when 'resubmit' then 'اعادة تقديم' 
END as action, c.new_time, Case co.status 
when 'free' then 'حر' 
when 'active' then 'نشط' 
when 'expired' then 'منتهي' 
when 'loan' then 'معار' 
when 'loan expire' then 'ااعارة منتهية' 
END as status, co.contract_id,c.feature_id 
FROM contract co 
INNER JOIN player_profile p ON p.player_id = co.player_id 
INNER JOIN new_request c ON c.contract_id = co.contract_id 
INNER JOIN club cl ON co.club_id = cl.club_id 
where co.reqeust_type='new' 
) as f1 
order by f1.player_id asc; 

數據集: 完整的數據集

Full Data Set

SELECT distinct(f1.player_id), f1.contract_id,f1.feature_id 
from (select p.player_id,p.player_name, p.player_no, CASE c.action 
when 'submit' then 'تقديم' 
when 'approve' then 'موافقة' 
when 'reject' then 'رفض' 
when 'object' then 'اعتراض' 
when 'resubmit' then 'اعادة تقديم' 
END as action, c.new_time, Case co.status 
when 'free' then 'حر' 
when 'active' then 'نشط' 
when 'expired' then 'منتهي' 
when 'loan' then 'معار' 
when 'loan expire' then 'ااعارة منتهية' 
END as status, co.contract_id,c.feature_id 
FROM contract co 
INNER JOIN player_profile p ON p.player_id = co.player_id 
INNER JOIN new_request c ON c.contract_id = co.contract_id 
INNER JOIN club cl ON co.club_id = cl.club_id 
where co.reqeust_type='new' 
) as f1 
group by f1.player_id 
order by f1.player_id asc; 

數據查詢
data set for query 2

我想設置獲得Player_ID 7和合同編號301

+0

究竟是什麼意思,它沒有按預期工作?請提供預期結果和2個查詢返回的結果! – Shadow

+0

請注意,DISTINCT不是一個函數。 – Strawberry

回答

1

考慮你的「基地」子查詢:

SELECT p.player_id,p.player_name, p.player_no, 
CASE c.action 
    when 'submit' then 'تقديم' 
    when 'approve' then 'موافقة' 
    when 'reject' then 'رفض' 
    when 'object' then 'اعتراض' 
    when 'resubmit' then 'اعادة تقديم' 
END as action, 
c.new_time, 
CASE co.status 
    when 'free' then 'حر' 
    when 'active' then 'نشط' 
    when 'expired' then 'منتهي' 
    when 'loan' then 'معار' 
    when 'loan expire' then 'ااعارة منتهية' 
END as status, 
co.contract_id, 
c.feature_id 
FROM contract co 
    INNER JOIN player_profile p ON p.player_id = co.player_id 
    INNER JOIN new_request c ON c.contract_id = co.contract_id 
    INNER JOIN club cl ON co.club_id = cl.club_id 
WHERE co.reqeust_type='new' 

如果你想獲得每個玩家最高contract_id,你應該做一個MAX(contract_id)GROUP BY球員,像這樣

SELECT MAX(co.contract_id), 
    p.player_id,p.player_name, p.player_no, 
CASE c.action 
    when 'submit' then 'تقديم' 
    when 'approve' then 'موافقة' 
    when 'reject' then 'رفض' 
    when 'object' then 'اعتراض' 
    when 'resubmit' then 'اعادة تقديم' 
END as action, 
c.new_time, 
CASE co.status 
    when 'free' then 'حر' 
    when 'active' then 'نشط' 
    when 'expired' then 'منتهي' 
    when 'loan' then 'معار' 
    when 'loan expire' then 'ااعارة منتهية' 
END as status, 
co.contract_id, 
c.feature_id 
FROM contract co 
    INNER JOIN player_profile p ON p.player_id = co.player_id 
    INNER JOIN new_request c ON c.contract_id = co.contract_id 
    INNER JOIN club cl ON co.club_id = cl.club_id 
WHERE co.reqeust_type='new' 
GROUP BY co.player_id 

然後,你可以從這個查詢只檢索玩家你想要的,就像這樣:

SELECT * 
FROM 
(
SELECT MAX(co.contract_id) AS contractid, 
    p.player_id,p.player_name, p.player_no, 
CASE c.action 
    when 'submit' then 'تقديم' 
    when 'approve' then 'موافقة' 
    when 'reject' then 'رفض' 
    when 'object' then 'اعتراض' 
    when 'resubmit' then 'اعادة تقديم' 
END as action, 
c.new_time, 
CASE co.status 
    when 'free' then 'حر' 
    when 'active' then 'نشط' 
    when 'expired' then 'منتهي' 
    when 'loan' then 'معار' 
    when 'loan expire' then 'ااعارة منتهية' 
END as status, 
co.contract_id, 
c.feature_id 
FROM contract co 
    INNER JOIN player_profile p ON p.player_id = co.player_id 
    INNER JOIN new_request c ON c.contract_id = co.contract_id 
    INNER JOIN club cl ON co.club_id = cl.club_id 
WHERE co.reqeust_type='new' 
GROUP BY co.player_id 
) T 
WHERE player_id = 7 
+0

謝謝托馬斯它的工作完美。 –

+0

@SyedAbdullahAli很高興閱讀。但是,如果它回答了這個問題,請將其標記爲已接受;) –