分组由多个条件组成 - MySQL [英] Group by - multiple conditions - MySQL

查看:257
本文介绍了分组由多个条件组成 - MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何组合2个条件组合?我有一天中每个小时的每个ID的记录,我想根据第一个ID和该天的那个ID的所有记录分组信息,然后第二个ID和该天的所有记录。

How can I combine 2 group by conditions? I have records for each id for every hour in a day and I want to group information by first id and all records for that id in that day then second id and all records for that in the day.

我的示例查询是:

SELECT
    r.name
  , r.network
  , r.namestring
  , i.name
, i.description
  , r.rid
  , i.id
  , d.dtime
  , d.ifInOctets
FROM router AS r
INNER JOIN interface AS i
ON r.rid = i.rid
INNER JOIN 1279080000_1_60 AS d
ON i.id = d.id
AND dtime BETWEEN 1279113600 AND 1279115400
WHERE r.network = "ITPN"
AND i.status = "active"
GROUP BY i.id AND d.dtime              // each id with all its dtime

该ID的聚合值。
任何想法我可以使用???我不想总结所有的值。

This always ends up giving me an aggregate value for that id. Any idea what I could use??? I don't want to sum up all values.

谢谢,

推荐答案

要通过多个表达式分组,您应该用逗号分隔它们,而不是与:

To group by multiple expressions you should separate them with a comma, not AND:

GROUP BY i.id, d.dtime

您还应确保同一天的两个记录具有相同的dtime值。从您的问题不清楚这是否是这种情况。

You should also ensure that two records form the same day have the same value of dtime. It's not clear from your question whether this is or is not the case.

这篇关于分组由多个条件组成 - MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆