[我在这个问题中使用了一个例子,我遇到的真正问题非常相似,但在这里写下它会太复杂了;)]
所以,我有一个数据库表:
id | text
-------+------------
1 | Google
2 | Yahoo
3 | Google
4 | Bing
5 | Yahoo
6 | Google
7 | Yahoo
8 | Google
Run Code Online (Sandbox Code Playgroud)
我想将它们全部选中,然后像这样显示它们:
Google 4
Yahoo 3
Bing 1
Run Code Online (Sandbox Code Playgroud)
所以我想按照数据库中的出现次数来排序这些短语...这听起来有点复杂......但我认为我的问题可以通过这个例子来理解.
所以我该怎么做?PHP/MySQL中适当的代码是什么?
select text, count(text)
from your_table
group by text
order by count(text) desc
Run Code Online (Sandbox Code Playgroud)
select text,count(*) AS cnt from there group by text order by cnt desc;
Run Code Online (Sandbox Code Playgroud)
像这样?