PHP SQL返回计数最高的字段

时间:2014-07-30 13:11:26

标签: php mysql sql sql-server

我需要一个返回包含最高计数的网站的查询。

请参阅下面的示例表:

字段:

id | site

1  | site1
2  | site2
3  | site3
4  | site3 

在上面这种情况下,它应该返回site3。

我该怎么做?

7 个答案:

答案 0 :(得分:1)

select site, count(id) from table group by site order by count(id) desc limit 1

应该工作

答案 1 :(得分:1)

SQL Server(SQL Fiddle):

SELECT TOP 1 site
FROM MyTable
GROUP BY site
ORDER BY COUNT(*) DESC

或MySQL(SQL Fiddle):

SELECT site 
FROM MyTable
GROUP BY site
ORDER BY COUNT(*) DESC
LIMIT 1

答案 2 :(得分:1)

试试这个:

SELECT site, 
         COUNT(site) TotalCount
    FROM table_name
GROUP BY site
  ORDER BY count(id) DESC LIMIT 1

答案 3 :(得分:0)

如果你想要字段网站,那么这里是查询:

SELECT COUNT(site), site FROM tab GROUP BY site DESC LIMIT 1

答案 4 :(得分:0)

这个应该有效

select max(C), S from (select count(id) C ,site S from table group by site) tab

答案 5 :(得分:0)

简单countgroup您的查询和order by计数列

SELECT 
      COUNT(id) as amount, 
      site 
FROM sitetable 

GROUP BY site 
ORDER BY amount DESC 
LIMIT 1

答案 6 :(得分:-1)

SELECT MAX(id)AS id,site FROM demo

它的归来 id site
4 site3