MYSQL Groupby 和 Orderby 查询

MYSQL Groupby and Orderby Query

我想根据特定 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;

数据集: 完整数据集

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

考虑您的 "base" 子查询:

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