简体   繁体   中英

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!

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.

When type is test this uses the primary key as a secondary grouping field, so all those rows will be in separate groups. For all other types, the secondary group will be equal for all items, so they don't get subgroups.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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