search

Home  >  Q&A  >  body text

mysql group by only displays one?

Assume user table

| id | name | group |
| 1 | evan | admin |
| 2| charlie | admin|
| 3 | kid | user |
| 4 | ward | user |
|5 | fy | user |
select * from user group by user.group
Why there are only 2 pieces of data selected, instead of dividing users whose group is admin into one group, and grouping users whose group is admin The users of user are divided into a group
How can mysql achieve the desired effect...
Currently the only thing I can think of is to take them all out, then traverse user.group as the key, and then re-obtain the group. Can the database directly achieve this?

仅有的幸福仅有的幸福2743 days ago1106

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-07-04 13:45:42

    How to group with mysql? Repeated

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-04 13:45:42

    mysql does not have it, just write one yourself, directly order by group to get the data, and then:
    pseudo code

    def map,arr, g
    for x in list
        if x.group != g
          g = x.group
          arr.new
          map.put(g,arr)
       arr.add(x)

    Collection operations in many languages ​​already include such functions, such as java8...

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-04 13:45:42

    This means dividing admin into one group and user into one group to get two pieces of data. I didn’t understand what the poster said

    reply
    0
  • Cancelreply