统计相同的order_id并在SQL中输出有多少个

发布于 2024-12-11 22:06:34 字数 375 浏览 0 评论 0原文

我想计算表 refnumbers 中有多少行具有相同的 order_id

因此它应该输出:

There's 5 rows with the order_id 123

There's 9 rows with the order_id 124

There's 18 rows with the order_id 125

There's 2 rows with the order_id 77

这是它应该在表中计数的列 order_is < code>refnumbers

如果没有特别提到 order_id,然后在 php 中对它们进行循环,我真的不知道如何做到这一点。

I would like to count how many rows there is of the same order_id in the table refnumbers

So it should output:

There's 5 rows with the order_id 123

There's 9 rows with the order_id 124

There's 18 rows with the order_id 125

There's 2 rows with the order_id 77

It's the column order_is that it should counter after in the table refnumbers

I dont really know how to do this without specifically mention a order_id and then do a loop through them in php.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

悟红尘 2024-12-18 22:06:34

您需要在要获取其计数的列上使用group by

这不起作用

SELECT count(*) as rowcount
FROM refnumbers

将为您提供包含所有行计数的单行

每个不同 order_id 的计数

SELECT order_id, count(*) as rowcount
FROM refnumbers
GROUP BY order_id /*WITH ROLLUP*/
ORDER BY order_id

将为您提供每个不同 order_id 的计数。
如果您想获取总计数,请取消注释 with rollup 部分。

You need to use group by on the column that you want to get the count for.

This does not work

SELECT count(*) as rowcount
FROM refnumbers

Will give you a single row with all rowcounts

Count per distinct order_id

SELECT order_id, count(*) as rowcount
FROM refnumbers
GROUP BY order_id /*WITH ROLLUP*/
ORDER BY order_id

Will give you the count per distinct order_id.
If you want to get the total count as well uncomment the with rollup part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文