我想根據與某個特定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;
數據集: 完整的數據集
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;
數據查詢
我想設置獲得Player_ID 7和合同編號301
究竟是什麼意思,它沒有按預期工作?請提供預期結果和2個查詢返回的結果! – Shadow
請注意,DISTINCT不是一個函數。 – Strawberry