使用 PHP 计算 MySQL 中相同行的数量

发布于 2024-11-19 16:05:19 字数 432 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我是有多爱你 2024-11-26 16:05:20

使用SELECT *, COUNT(*) AS cnt FROM keywords GROUP BY 查询

Use SELECT *, COUNT(*) AS cnt FROM keywords GROUP BY query.

巴黎盛开的樱花 2024-11-26 16:05:20
SELECT *, count(1) FROM keywords GROUP BY query
SELECT *, count(1) FROM keywords GROUP BY query
栀子花开つ 2024-11-26 16:05:20
SELECT query, COUNT(query) FROM keywords GROUP BY query
SELECT query, COUNT(query) FROM keywords GROUP BY query
过气美图社 2024-11-26 16:05:20
SELECT keyword, COUNT(*) FROM keywords GROUP BY keyword;
SELECT keyword, COUNT(*) FROM keywords GROUP BY keyword;
慕巷 2024-11-26 16:05:19

尝试这个查询:

SELECT query, COUNT(1) AS rpt_count FROM keywords GROUP BY query

在 PHP 中,您将使用 $update['query']$update['rpt_count'] 访问列

Try this query:

SELECT query, COUNT(1) AS rpt_count FROM keywords GROUP BY query

and in PHP you would access the columns using $update['query'] and $update['rpt_count']

旧人哭 2024-11-26 16:05:19
SELECT *, count(*) as cnt FROM keywords GROUP BY query
SELECT *, count(*) as cnt FROM keywords GROUP BY query
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文