MYSQLselect from group by

Posted AlexBai326

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQLselect from group by相关的知识,希望对你有一定的参考价值。

  • group by
mysql> select * from t_user group by lvl;
+-----+-------+----------+------+--------+
| uid | uname | tel      | lvl  | salart |
+-----+-------+----------+------+--------+
|   1 | alex  | 23131231 | A    |   1100 |
|   3 | kit   | 87416574 | B    |   1200 |
|   4 | poker | 41874521 | C    |   1560 |
+-----+-------+----------+------+--------+
mysql> select lvl from t_user group by lvl;
+------+
| lvl  |
+------+
| A    |
| B    |
| C    |
+------+
  • group by + group_concat()
mysql> select lvl,group_concat(salart) from t_user group by lvl;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| A    | 1100,4300            |
| B    | 1200                 |
| C    | 1560                 |
+------+----------------------+
3 rows in set (0.00 sec)

mysql> select lvl,group_concat(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    | 1,2    |
| B    | 3      |
| C    | 4      |
+------+--------+
3 rows in set (0.00 sec)

mysql> select lvl,group_concat(salart) from t_user group by lvl;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| A    | 1100,4300            |
| B    | 1200                 |
| C    | 1560                 |
+------+----------------------+
3 rows in set (0.00 sec)

mysql> select lvl,group_concat(salart) as salary from t_user group by lvl;
+------+-----------+
| lvl  | salary    |
+------+-----------+
| A    | 1100,4300 |
| B    | 1200      |
| C    | 1560      |
+------+-----------+
3 rows in set (0.00 sec)
  • #group by + 集合函数
mysql> select lvl,group_concat(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    | 1,2    |
| B    | 3      |
| C    | 4      |
+------+--------+
3 rows in set (0.00 sec)

mysql> select lvl,count(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    |      2 |
| B    |      1 |
| C    |      1 |
+------+--------+
3 rows in set (0.00 sec)
  • #group by + having
mysql> select lvl,group_concat(salart) from t_user group by lvl having group_concat(salart)=1200;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| B    | 1200                 |
+------+----------------------+
1 row in set (0.00 sec)

mysql> select lvl,group_concat(salart) from t_user group by lvl having group_concat(salart)=1100;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| A    | 1100,4300            |
+------+----------------------+
1 row in set (0.00 sec)

mysql> 
mysql> select lvl,count(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    |      2 |
| B    |      1 |
| C    |      1 |
+------+--------+
3 rows in set (0.00 sec)

mysql> select lvl,count(uid) as salary from t_user group by lvl having count(uid)>1;
+------+--------+
| lvl  | salary |
+------+--------+
| A    |      2 |
+------+--------+
1 row in set (0.00 sec)

 

以上是关于MYSQLselect from group by的主要内容,如果未能解决你的问题,请参考以下文章

MySQL SELECT * FROM ... WHERE ... ORDER BY '哪一个在另一个表中有最新记录'

MYSQL SELECT 与 GROUP。想要限制 GROUP BY 中返回元素的数量

MySQL之分组查询(GROUP BY)

MySQL SELECT 从多个表,多个 GROUP BY 和 group_concat?

查询语句中select from where group by having order by的执行顺序

GROUP BY DAY(FROM_UNIXTIME) 意外更改计数