查找每个 GROUP BY 结果中的行数

发布于 2025-01-03 04:53:40 字数 453 浏览 0 评论 0原文

我有一个数据库,可以告诉每个系统当前和新的型号。 (每一行都有系统名称、当前模型和新模型)我需要按当前模型和新模型对结果进行分组。因此,如果当前模型和新模型相同,我想要一个结果。我已经做到了这一点,但我还需要知道每组中有多少个系统。我怎样才能做到这一点?我正在使用 CI

这是我的查询

$query2 = $this->db->query('SELECT * FROM rollout_systems WHERE scope_ID = '.$id.'  GROUP BY EAM_Model, new_Model');

(EAM_Model = 当前模型)

不知道我是否澄清得足够多。我需要能够显示当前系统、新系统以及该组中的系统数量。

因此,如果 3 行有“blah”作为当前系统,“blahblah”作为新系统,我想说 当前: 布拉 新:巴拉巴拉 系统数量:3

I have a database the tells each systems current and new model. (Each row has a system name, current model, and new model) I need to group the results by the current model and new model. So if the current and new model are the same i want one result. I got that to work but i also need to know how many systems are in each group. How can I do that? Im using CI

This is my query

$query2 = $this->db->query('SELECT * FROM rollout_systems WHERE scope_ID = '.$id.'  GROUP BY EAM_Model, new_Model');

(EAM_Model = current model)

Don't know if i clarified enough. I need to be able to display the Current System, the New System, and the number of systems in that group.

So if 3 rows have "blah" as there current system and "blahblah" as there new system, i want it to say
Current: Blah
New: Blah Blah
number of systems: 3

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

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

发布评论

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

评论(3

简单 2025-01-10 04:53:40

在同一个语句中执行 select * 和 group by 是没有意义的。您分组所依据的列需要位于您的选择中,但您的选择中应该有一个聚合函数,例如 count(*) 。我不确定我完全理解你想要做什么,但我认为你想要类似的东西:

SELECT EAM_Model, new_Model, count(*)
FROM rollout_systems 
WHERE scope_ID = '.$id.'              
GROUP BY EAM_Model, new_Model

It doesn't make sense to do a select * and a group by in the same statement. The columns you group by need to be in your select but then you should have an aggregate function like count(*) in your select. I'm not sure I totally understand what you are trying to do, but I think you want something like:

SELECT EAM_Model, new_Model, count(*)
FROM rollout_systems 
WHERE scope_ID = '.$id.'              
GROUP BY EAM_Model, new_Model
夜吻♂芭芘 2025-01-10 04:53:40

如果您只需要项目数量,请使用 SELECT EAM_Model, new_Model, COUNT(*) FROM ...等。对查询进行分组时,您通常希望执行 COUNT 或 SUM 等聚合否则对数据进行分组就没有意义。

If you just want the number of items, use SELECT EAM_Model, new_Model, COUNT(*) FROM ... etc. When you group a query, you normally want to be performing an aggregate like COUNT or SUM or else it doesn't make sense to be grouping the data.

灵芸 2025-01-10 04:53:40

$query2 = $this->db->query('SELECT count(*) FROM rollout_systems WHERE range_ID = '.$id.' GROUP BY EAM_Model, new_Model');

你要求的是这个吗?

$query2 = $this->db->query('SELECT count(*) FROM rollout_systems WHERE scope_ID = '.$id.' GROUP BY EAM_Model, new_Model');

Is it this you are asking for?

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