How to group in mysql?
PHP中文网
PHP中文网 2017-07-04 13:43:47
0
2
1072

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

Solution,

mysql How to group arrays? It feels like there are many places where grouping is needed.

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
伊谢尔伦

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 of group data: admin and user.

Grouping needs to be used in conjunction with statistical methods such as count, sum etc.

If you want the data of admin to be together and the data of user to be together, then just order by user.group is fine

扔个三星炸死你

If 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. For GROUP BY user.group, you only have two unique values: user and admin, 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 losing user.namedata, you can use the GROUP_CONCAT() function to merge the name in all groups into a comma-separated string ( Of course it can be changed to other separators)

SELECT *, GROUP_CONCAT(user.name) FROM user GROUP BY user.group
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template