MYSQL Groupby和Orderby Query

时间:2016-05-17 08:21:15

标签: mysql group-by sql-order-by

我希望根据特定player_id以尽可能最高的contract_id对数据进行分组。 这听起来很简单但是没有按预期工作。

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和合同ID 301。

1 个答案:

答案 0 :(得分: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