2016-05-17 11 views
-1

Ich möchte nach den Daten mit der höchstmöglichen contract_id in Bezug auf eine bestimmte player_id gruppieren. Es klingt einfach genug, aber es funktioniert nicht wie erwartet.MYSQL Groupby und Orderby Query

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; 

Data Set: Full Data Set

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; 

Datensatz für die Suche nach
data set for query 2

Ich möchte Player_id 7 und Vertrags-ID 301.

+0

Was genau meinen Sie damit, dass es nicht wie erwartet funktioniert? Bitte geben Sie das erwartete Ergebnis und das Ergebnis der 2 Abfragen zurück! – Shadow

+0

Beachten Sie, dass DISTINCT keine Funktion ist. – Strawberry

Antwort

1

Betrachten Sie Ihre „Basis“ Unterabfrage zu erhalten:

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' 

Wenn Sie die höchste contract_id pro Spieler erhalten möchten, sollten Sie einen MAX(contract_id) und GROUP BY Spieler tun, wie diese

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 

Dann können Sie nur dem Spieler Ihren wollen aus dieser Abfrage, wie folgt ab:

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

Danke thomas es hat perfekt funktioniert. –

+0

@SyedAbdullahAli froh zu lesen. Aber wenn es die Frage beantwortet, markieren Sie es bitte als akzeptiert;) –