1,分組查詢
select <列名>, count(*) from <表名> group by <列名>;
查詢每個班級的人數
MariaDB [mydb]> select class, count(*) from score group by class;
+-------+----------+
| class | count(*) |
+-------+----------+
| 1 | 4 |
| 2 | 1 |
| 3 | 2 |
+-------+----------+
3 rows in set (0.001 sec)
查詢人數大於2的班級
MariaDB [mydb]> select class, count(*) from score group by class having count(*)>2;
+-------+----------+
| class | count(*) |
+-------+----------+
| 1 | 4 |
+-------+----------+
1 row in set (0.002 sec)
查詢數學大於80的各班人數
MariaDB [mydb]> select class, count(*) from score where math > 80 group by class;
+-------+----------+
| class | count(*) |
+-------+----------+
| 1 | 3 |
| 3 | 1 |
+-------+----------+
查詢數學大於80並且人數大於1的班級
MariaDB [mydb]> select class, count(*) from score where math > 80 group by class having count(*)>1;
+-------+----------+
| class | count(*) |
+-------+----------+
| 1 | 3 |
+-------+----------+
1 row in set (0.002 sec)
查詢每個班下的男生和女生人數
MariaDB [mydb]> select * from score;
+-------+-------+-----------+------+------+----------+--------+
| id | class | name | sex | math | language | sports |
+-------+-------+-----------+------+------+----------+--------+
| 20001 | 1 | 小明 | 男 | 81 | 86 | 93 |
| 20002 | 3 | 小紅 | 女 | 86 | 86 | 89 |
| 20003 | 2 | 小張 | 男 | 77 | 83 | 93 |
| 20004 | 1 | 露絲 | 女 | 88 | 78 | 65 |
| 20005 | 1 | 麗麗 | 女 | 92 | 94 | 64 |
| 20006 | 3 | 李明 | 男 | 75 | 78 | 88 |
| 20007 | 1 | 張大明 | 男 | 54 | 65 | 95 |
+-------+-------+-----------+------+------+----------+--------+
7 rows in set (0.000 sec)
MariaDB [mydb]> select class, sex, count(*) from score group by class,sex;
+-------+------+----------+
| class | sex | count(*) |
+-------+------+----------+
| 1 | 女 | 2 |
| 1 | 男 | 2 |
| 2 | 男 | 1 |
| 3 | 女 | 1 |
| 3 | 男 | 1 |
+-------+------+----------+
5 rows in set (0.001 sec
MariaDB [mydb]> select class, sex, count(*) from score group by class,sex having count(*) > 1;
+-------+------+----------+
| class | sex | count(*) |
+-------+------+----------+
| 1 | 女 | 2 |
| 1 | 男 | 2 |
+-------+------+----------+
2 rows in set (0.002 sec)
2,分頁顯示
select * from <表名> limit <起始位置>, <顯示條數>;
MariaDB [mydb]> select * from score;
+-------+-------+-----------+------+------+----------+--------+
| id | class | name | sex | math | language | sports |
+-------+-------+-----------+------+------+----------+--------+
| 20001 | 1 | 小明 | 男 | 81 | 86 | 93 |
| 20002 | 3 | 小紅 | 女 | 86 | 86 | 89 |
| 20003 | 2 | 小張 | 男 | 77 | 83 | 93 |
| 20004 | 1 | 露絲 | 女 | 88 | 78 | 65 |
| 20005 | 1 | 麗麗 | 女 | 92 | 94 | 64 |
| 20006 | 3 | 李明 | 男 | 75 | 78 | 88 |
| 20007 | 1 | 張大明 | 男 | 54 | 65 | 95 |
+-------+-------+-----------+------+------+----------+--------+
7 rows in set (0.000 sec)
MariaDB [mydb]> select * from score limit 2,3;
+-------+-------+--------+------+------+----------+--------+
| id | class | name | sex | math | language | sports |
+-------+-------+--------+------+------+----------+--------+
| 20003 | 2 | 小張 | 男 | 77 | 83 | 93 |
| 20004 | 1 | 露絲 | 女 | 88 | 78 | 65 |
| 20005 | 1 | 麗麗 | 女 | 92 | 94 | 64 |
+-------+-------+--------+------+------+----------+--------+
3 rows in set (0.001 sec)
select 列名 from 表名
where 限定條件
group by 分組的列
[having 分組之後的篩選條件]
order by 排序的列 desc/asc
例如:
select studsex,schoolno ,count(*) from studentnew where studteacher ='李老師' GROUP BY schoolno,studsex HAVING count(*)>5;
select * from 表名
where 篩選條件
geoup by --> 分組
having --> 分組後的篩選條件
order by 排序的列 asc/desc
limit 1,5開始位置,顯示條數
注:where,order by ,limit 可以單獨出現,也可以組合出現