SQL语句高级(一)

一、
mysql> select cno,avg(degree),count(*)   //新表字段名 
from score 							//原始表
group by cno 						//以cno字段分组
having count(cno) >= 2       //该字段的数据总数大于2
and 										//条件与
cno like '3%';                       //模糊查询
+-------+-------------+----------+
| cno   | avg(degree) | count(*) |
+-------+-------------+----------+
| 3-105 |     86.0000 |        3 |
| 3-245 |     86.2000 |        5 |
+-------+-------------+----------+

二、
mysql> select sno,degree from score where degree between 70 and 90;
也可以写成:
mysql> select sno,degree from score where degree > 70 and degree < 90;
+-----+--------+
| sno | degree |
+-----+--------+
| 103 |     89 |
| 104 |     89 |
| 105 |     89 |
| 105 |     89 |
+-----+--------+

三、
mysql> select sno,sname from student;
+-----+--------+
| sno | sname  |
+-----+--------+
| 100 | Java   |
| 101 | C      |
| 102 | C++    |
| 103 | C#     |
| 104 | Python |
| 105 | JS     |
+-----+--------+
6 rows in set (0.00 sec)

mysql> select sno,cno,degree from score;
+-----+--------+--------+
| sno | cno    | degree |
+-----+--------+--------+
| 100 | 3-105  |    100 |
| 100 | 3-245  |     55 |
| 101 | 6-1166 |     59 |
| 102 | 3-245  |    100 |
| 103 | 3-105  |     99 |
| 103 | 3-245  |     89 |
| 103 | 6-1166 |     59 |
| 104 | 3-105  |     59 |
| 104 | 3-245  |     98 |
| 104 | 6-1166 |     89 |
| 105 | 3-245  |     89 |
| 105 | 6-1166 |     89 |
+-----+--------+--------+
12 rows in set (0.00 sec)
上面两条的综合查询(多表查询!!):

mysql> select sname,cno,degree from student,score where student.sno = score.sno;
+--------+--------+--------+
| sname  | cno    | degree |
+--------+--------+--------+
| Java   | 3-105  |    100 |
| Java   | 3-245  |     55 |
| C      | 6-1166 |     59 |
| C++    | 3-245  |    100 |
| C#     | 3-105  |     99 |
| C#     | 3-245  |     89 |
| C#     | 6-1166 |     59 |
| Python | 3-105  |     59 |
| Python | 3-245  |     98 |
| Python | 6-1166 |     89 |
| JS     | 3-245  |     89 |
| JS     | 6-1166 |     89 |
+--------+--------+--------+

猜你喜欢

转载自blog.csdn.net/qq_37150711/article/details/87185957