标签:使用 order 结果 大于 假设 order by mysql 次数 desc
练习使用 group by having... 语句
假设有 student 表,如下:
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | 刘备 | 23 |
| 2 | 关羽 | 22 |
| 3 | 张飞 | 21 |
| 4 | 刘表 | 43 |
| 5 | 刘璋 | 43 |
| 6 | 刘蝉 | 3 |
| 7 | 曹操 | 33 |
| 8 | 曹植 | 13 |
| 9 | 曹丕 | 15 |
| 10 | 关平 | 17 |
+----+------+-----+
解:select left(name, 1) from student group by left(name, 1) having count(*)<3;
得到结果集:
+---------------+
| left(name, 1) |
+---------------+
| 关 |
| 张 |
+---------------+
解:select left(name, 1) from student group by left(name, 1) having count(*)>3;
得到结果集:
+---------------+
| left(name, 1) |
+---------------+
| 刘 |
+---------------+
解:select left(name, 1) from student group by left(name, 1) having count(*)>1 and count(*)<4;
得到结果集:
+---------------+
| left(name, 1) |
+---------------+
| 关 |
| 曹 |
+---------------+
解:select left(name, 1), count(*) from student group by left(name, 1);
得:
+---------------+----------+
| left(name, 1) | count(*) |
+---------------+----------+
| 关 | 2 |
| 刘 | 4 |
| 张 | 1 |
| 曹 | 3 |
+---------------+----------+
解:select left(name, 1), count(*) from student group by left(name, 1) order by count(*) desc;
得:
+---------------+----------+
| left(name, 1) | count(*) |
+---------------+----------+
| 刘 | 4 |
| 曹 | 3 |
| 关 | 2 |
| 张 | 1 |
+---------------+----------+
标签:使用 order 结果 大于 假设 order by mysql 次数 desc
原文地址:https://www.cnblogs.com/wumingoo1/p/14033976.html