【MYSQL】select 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)

 

转载于:https://www.cnblogs.com/AlexBai326/p/6048918.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值