2012-02-08 79 views
0

[我正在使用這個問題的例子,真正的問題我已經是非常相似的,但它是太複雜,它寫下來這裏;)]如何選擇記錄並將COUNT顯示文字?

所以,我有一個數據庫表:

id | text 
-------+------------ 
    1 | Google 
    2 | Yahoo 
    3 | Google 
    4 | Bing 
    5 | Yahoo 
    6 | Google 
    7 | Yahoo 
    8 | Google 

我想將其全部選中,然後像這樣顯示出來:

Google 4 
Yahoo 3 
Bing 1 

所以,我想通過外表的數據庫數量訂購短語...... 這聽起來有點複雜。 ..但我認爲我的問題可以用exapmle來理解。

那我該怎麼辦? PHP/MySQL中的適當代碼是什麼?

+3

請更新帖子的標題,這不是很明確的... – 2012-02-08 16:00:28

+1

@tsabz:我試圖提供更具描述性的標題。如果任何人都可以改進它,請放心。 – 2012-02-08 16:02:31

+1

現在好多了。 – 2012-02-08 16:03:35

回答

7
select text, count(text) 
from your_table 
group by text 
order by count(text) desc 
+1

這裏沒有問題,但請注意,與count(*)不同,這個'count(text)'用非null'text'來計數行。 – 2012-02-08 16:21:40

6
select text,count(*) AS cnt from there group by text order by cnt desc; 

是這樣的?

1
SELECT text, COUNT(*) FROM your_table 
GROUP BY text 
ORDER BY COUNT(*) DESC 
4
SELECT text, COUNT(id) AS ordering 
FROM table 
GROUP BY text 
ORDER BY ordering DESC 
1
select count(id) as count, text from table group by text order by count desc 

我沒有嘗試,但可能是正確的