简体   繁体   English

忽略MySQL按值分组

[英]Ignore MySQL Group By value

basically I'm having some troubles trying to group all rows by their type except the only which is called lets say "test", this one is meant to be ignored but displayed apart if its possible, but I can't really find a solution, thanks in advance! 基本上我在尝试按类型对所有行进行分组时遇到了一些麻烦,除了唯一的让我们说“ test”的行,这是可以忽略的,但如果可能的话分开显示,但是我找不到真正的解决方案, 提前致谢!

SELECT *, SUM(amount) as 'amount' FROM history WHERE date = '$date' GROUP BY type

The query which is above is grouping all rows including the one I'm trying to ignore 上面的查询将所有行分组,包括我要忽略的行

You can use: 您可以使用:

GROUP BY type, IF(type = "test", id, 0)

Replace id with the primary key of the table. 用表的主键替换id

When type is test this uses the primary key as a secondary grouping field, so all those rows will be in separate groups. test type时,它将主键用作辅助分组字段,因此所有这些行将位于单独的组中。 For all other types, the secondary group will be equal for all items, so they don't get subgroups. 对于所有其他类型,次要组对于所有项目都是相等的,因此它们不会获得子组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM