MYSQL Groupby和Orderby查询

MYSQL Groupby和Orderby查询,mysql,group-by,sql-order-by,Mysql,Group By,Sql Order By,我想根据特定球员的合同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 'ر

我想根据特定球员的合同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;

用于查询的数据集

我想获得玩家ID 7和合同ID 301。

考虑您的“基本”子查询:

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' 
如果您希望每个玩家获得最高的
合同id
,您应该执行
MAX(合同id)
按玩家分组,如下所示

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

你说它没有按预期工作到底是什么意思?请提供预期结果和2个查询返回的结果!请注意,DISTINCT不是一个函数。@SyedAbdullahAli很高兴阅读。但如果它回答了问题,请将其标记为已接受;)