我只是想知道,如何获得表中最大的数字。我的意思是,我有一张桌子叫:hits;因为它们是2列:1.id2.命中
他们有很多id在表中,并且都有超过10次点击,现在我想做的是获得最大点击中的最大idPS:见下文:
id | hit
---|----
1 | 10
2 | 15
3 | 45
4 | 9
是的,您可以使用MAX function
来使用以下
Select Id,Max(hit) from yourtableName group by id having hit=Max(hit)
Select Id,
Max(Hit)
from tableName
group by id
having Max(hit)=(Select Max(Hit) from TableName)
SQL FIDDLE演示
这样做不是更快吗:
SELECT * FROM table WHERE 1 ORDER BY hit DESC, id DESC LIMIT 1
而不是使用MAX
,特别是如果你有一个更大的表
http://www.witti.ws/blog/2011/04/06/mysqls-max-slow-5-years-later