2010-06-14 63 views
1

我的表结构是(ID,集群,QID,优先级)。我试图弄清楚如何显示每个群集的最大优先级值。说群集1有优先级100,102,105。我想显示包含105的记录。请帮助。MySQL查询问题上组和最大

+0

请发表您已有的 – 2010-06-14 08:10:56

回答

2
select cluster, MAX(priority) from structure group by cluster; 

要查找所有的列TRY

select * from structure 
where priority = (
       select MAX(priority) from structure as s 
       where s.cluster = structure.cluster 
      ); 
+0

不,这是行不通的。它不返回集群最高优先级的记录。只显示最大值。 – 1s2a3n4j5e6e7v 2010-06-14 08:14:30

+0

好吧,然后尝试我编辑的答案。 – Salil 2010-06-14 08:21:33

2

您可以用内筛选出的行加入,如:

select s.* 
from structure s 
join (
     select cluster, MAX(priority) maxprio 
     from structure 
     group by 
       cluster 
     ) filter 
on  s.cluster = filter.cluster 
     and s.priority = filter.maxprio 

这将返回多行如果它们都具有该群集的最高优先级。

+0

这个工程!感谢您的直接帮助。与逻辑http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/相比,这似乎有点慢。如果您愿意,请尝试检查。 – 1s2a3n4j5e6e7v 2010-06-14 08:41:05