为所有用户按数字字段选择最大相似行

时间:2011-04-07 08:24:25

标签: mysql select

我们有如下表格:

  user    point
+-------+------+  
| user1 | 6080 | // user1 has this value  
+-------+------+  
| user1 | 5300 |  
+-------+------+  
| user2 | 6080 | // user2 has this value  
+-------+------+  
| user3 | 6080 | // user3 has this value  
+-------+------+  
| user3 | 5300 |  
+-------+------+  
| user1 | 3520 |  
+-------+------+  
| user2 | 5300 |  
+-------+------+  
| user3 | 9800 |  

我希望得到所有users至少有一个这样的最大相似值 在此示例中,所有用户都有两个值。 60805300

结果应为6080

我该怎么做?

2 个答案:

答案 0 :(得分:4)

看起来像这样。您需要将结果限制为1,因此例如对于mysql,您应该在结尾使用限制1或者使用mssql选择前1点等等。

select point
from user
group by point
having count(*)=(select count(distinct user) from user)
order by point desc

答案 1 :(得分:0)

SELECT point, count(*) AS cnt FROM table GROUP BY point ORDER BY cnt DESC LIMIT 1