Home > database >  Mysql query a table field in the table, the number of occurrences of b and sorted
Mysql query a table field in the table, the number of occurrences of b and sorted

Time:10-17

For example the user is a user table, MSG is information table
The user
Id name
1 a
2 b

MSG
Id uid content
1
1 qq2 1 ss
3 2 dd

Now I just want to query the user information and their speech, and can be sorted by the number of statistics, which is below the result set;
how to write a statement?Id name count
1 a 2
2 b 1

CodePudding user response:

The SELECT m.u id as id, u.n ame, count (m.u id) from ` user ` u, MSG m WHERE u.i d=m.u id group by uid

CodePudding user response:

The select t.i d id, t.n ame name, count (m.c ontent) CNT
From the user t, MSG m
Where t.i d=m.u id
Group by t.i d
The order by CNT.
  • Related