【发布时间】:2010-06-14 08:07:55
【问题描述】:
我的表结构是 (id,cluster,qid,priority)。我试图弄清楚如何显示每个集群的最大优先级值。假设集群 1 的优先级为 100、102、105。我想显示包含 105 的记录。请帮忙。
【问题讨论】:
-
请张贴你已经拥有的东西
标签: mysql group-by max aggregate-functions
我的表结构是 (id,cluster,qid,priority)。我试图弄清楚如何显示每个集群的最大优先级值。假设集群 1 的优先级为 100、102、105。我想显示包含 105 的记录。请帮忙。
【问题讨论】:
标签: mysql group-by max aggregate-functions
这里有一篇文章解释了如何为每个组选择具有最大值的行。
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
【讨论】:
select cluster, MAX(priority) from structure group by cluster;
要查找所有列,请尝试
select * from structure
where priority = (
select MAX(priority) from structure as s
where s.cluster = structure.cluster
);
【讨论】:
您可以使用内部连接过滤掉行,例如:
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
如果它们都具有该集群的最大优先级,这将返回多行。
【讨论】: