查找每个 GROUP BY 结果中的行数
我有一个数据库,可以告诉每个系统当前和新的型号。 (每一行都有系统名称、当前模型和新模型)我需要按当前模型和新模型对结果进行分组。因此,如果当前模型和新模型相同,我想要一个结果。我已经做到了这一点,但我还需要知道每组中有多少个系统。我怎样才能做到这一点?我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在同一个语句中执行 select * 和 group by 是没有意义的。您分组所依据的列需要位于您的选择中,但您的选择中应该有一个聚合函数,例如 count(*) 。我不确定我完全理解你想要做什么,但我认为你想要类似的东西:
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 ...等。对查询进行分组时,您通常希望执行 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.$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?