使用 PHP 计算 MySQL 中相同行的数量
我在 MySql 中有一个表,其中包含关键字列表。用户在此站点上输入后,每个关键字都会存储在一个新行中。
我在 PHP 中有以下查询:
SELECT * FROM keywords GROUP BY query
它从 MySql 获取所有关键字,并且仅显示重复关键字的每个关键字之一。所以输出是这样的:
Dog
Cat
Lion
Fong
当我使用 $update['query'];
但我想计算每个关键字在数据库中出现的次数,所以输出是,例如:
Dog (2)
Cat (3)
Lion (1)
Fong (1)
我试图弄清楚 SQL 查询应该是什么,以及如何使用 PHP 打印它。
I have a table in MySql with a list of keywords. Each keyword was stored in a new row once it was entered by a user on this site.
I have the following query in PHP:
SELECT * FROM keywords GROUP BY query
Which gets all the keywords from MySql, and only shows one of each incase of duplicate keywords. So the output is something like:
Dog
Cat
Lion
Fong
When I'm using $update['query'];
But I'd like to count how many times each keyword appears in the database, so the output would be, for example:
Dog (2)
Cat (3)
Lion (1)
Fong (1)
And I'm trying to figure out what the SQL query should be, and how to print it using PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
SELECT *, COUNT(*) AS cnt FROM keywords GROUP BY 查询
。Use
SELECT *, COUNT(*) AS cnt FROM keywords GROUP BY query
.尝试这个查询:
在 PHP 中,您将使用
$update['query']
和$update['rpt_count']
访问列Try this query:
and in PHP you would access the columns using
$update['query']
and$update['rpt_count']