Assume user table
id | name | group |
---|---|---|
1 | evan | admin |
1 | evan1 | admin |
1 | evan2 | admin |
1 | evan3 | user |
1 | evan4 | user |
select * from user group by user.group
There are only 2 pieces of data coming out, instead of user.group
being a group of admin
, user.group
is a group of user
mysql How to group arrays? It feels like there are many places where grouping is needed.
I don’t quite understand what you mean. If you use
group by user.group
,2
items will appear, because your data only has two kinds ofgroup
data:admin
anduser
.Grouping
needs to be used in conjunction with statistical methods such ascount
,sum
etc.If you want the data of
admin
to be together and the data ofuser
to be together, then justorder by user.group
is fineIf you use the
GROUP BY
clause, if there is only one condition, only all unique values that satisfy the condition will be used, one for each piece of data. ForGROUP BY user.group
, you only have two unique values:user
andadmin
, so there will only be two pieces of data.If you want to put the same
user.group
data together, as mentioned above, just use sorting.If you want to merge the same
user.group
into one row without losinguser.name
data, you can use theGROUP_CONCAT()
function to merge thename
in all groups into a comma-separated string ( Of course it can be changed to other separators)