Java基础入门day39

day39

DQL

查询结果去重

语法:

distinct 列明

select distinct sex from tb_stu;        //  性别去重,最终只保留两个结果boy和girl
select distinct name, sex from tb_stu;  //  姓名和性别同时去重,sex部分字段值相同,不能去重,去重只能是所有字段都相同才可以实现去重效果,只有                                          //      name和sex都相同才会实现去重效果
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
+------+--------------+------+------+-------+------+
3 rows in set (0.01 sec)
​
mysql> select sex from tb_stu;
+------+
| sex  |
+------+
| boy  |
| girl |
| girl |
+------+
3 rows in set (0.00 sec)
​
mysql> select distinct sex from tb_stu;
+------+
| sex  |
+------+
| boy  |
| girl |
+------+
2 rows in set (0.00 sec)
​
mysql> select name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
+--------------+------+
3 rows in set (0.00 sec)
​
mysql> select distinct name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
+--------------+------+
3 rows in set (0.00 sec)
​
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)
​
mysql> insert into tb_stu values(9530, 'qiuxiang', 'girl', 110, 80, 18);
Query OK, 1 row affected (0.01 sec)
​
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)
​
mysql> select name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
| qiuxiang     | girl |
+--------------+------+
4 rows in set (0.00 sec)
​
mysql> select distinct name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
+--------------+------+
3 rows in set (0.00 sec)

排序

语法:

select 列名 from 表名 order by 排序列 [排序规则]

单列排序

select * from tb_stu order by score;                //  根据成绩来排序,默认排序规则是升序排列 
select * from tb_stu order by score asc;            //  asc是升序关键字,默认就是升序,可以不写
select * from tb_stu order by score desc;           //  desc降序排序的关键字
排序规则描述
asc对前面排序列做升序排序,也是默认值
desc对前面排序列做降序排列

多列排序

select * from tb_stu order by sex desc, score;
select * from tb_stu order by sex desc, score asc;
select * from tb_stu order by sex desc, score desc;
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from tb_stu order by sex;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from tb_stu order by sex asc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from tb_stu order by sex desc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from tb_stu order by sex desc, score;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from tb_stu order by sex desc, score desc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from tb_stu order by sex desc, score asc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

条件查询

语法:
​
select 列名 from 表名 where 条件
关键字描述
where条件在查询结果中,筛选符合条件的查询结果,条件为布尔表达式
  • 等值判断

select * from tb_stu where age = 30;        //  查询age为30的学生
注意: 与Java的==不同,mysql中等值判断使用=
  • 逻辑判断(and or not)

select * from tb_stu where age = 30 and sex = 'girl';			//	and表示交集,得同时满足,查询age为30并且sex同时为girl结果
select * from tb_stu where age = 30 or sex = 'girl';			//	or表示并集,任何条件满足都可以,查询age为30或者sex为girl的所有结果
select * from tb_stu where not age = 30;						//	not取反,对于现有结果取反操作,年龄不为30的所有结果
mysql> select * from tb_stu where age = 30 and sex = 'girl';
+------+--------+------+------+-------+------+
| sid  | name   | sex  | tel  | score | age  |
+------+--------+------+------+-------+------+
| 9529 | shiliu | girl | 114  |    59 |   30 |
+------+--------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where age = 30 or sex = 'girl';
+------+----------+------+------+-------+------+
| sid  | name     | sex  | tel  | score | age  |
+------+----------+------+------+-------+------+
| 9528 | qiuxiang | girl | 110  |    80 |   18 |
| 9529 | shiliu   | girl | 114  |    59 |   30 |
| 9530 | qiuxiang | girl | 110  |    80 |   18 |
+------+----------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where not age = 30;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)
  • 不等值判断(> >= < <= != <>)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu where sex < 60;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set, 5 warnings (0.00 sec)

mysql> select * from tb_stu where score < 60;
+------+--------+------+------+-------+------+
| sid  | name   | sex  | tel  | score | age  |
+------+--------+------+------+-------+------+
| 9529 | shiliu | girl | 114  |    59 |   30 |
+------+--------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where score >= 60;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu where score = 100;
Empty set (0.00 sec)

mysql> select * from tb_stu where score != 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu where score <> 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)
mysql> select * from tb_stu where score <= 100 and score >= 80;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)
  • 区间判断(between and)

mysql> select * from tb_stu where score between 60 and 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 80 and 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 80 and 99;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 180 and 99;
Empty set (0.00 sec)

注意:在区间判断的语法中,小值在前,大值在后,反之将得不到结果

  • null值判断(is null, is not null)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select * from tb_stu where name is null;
Empty set (0.00 sec)

mysql> select * from tb_stu where sex is null;
+------+-----------+------+------+-------+------+
| sid  | name      | sex  | tel  | score | age  |
+------+-----------+------+------+-------+------+
| 9532 | chunxiang | NULL | NULL |  NULL | NULL |
+------+-----------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where sex is not null;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

判断某个字段是否为空,或者是否不为空

  • 枚举查询(in(值1, 值2, ...))

select * from tb_stu where sid in (9527, 9528, 9599);			//	查询sid分别为9527, 9528和9599的值,9599不存在,则不显示,也不报																  //		错,只列出9527 和9528的值

注意:in的查询效率较低,可以通过多条件拼接

  • 模糊查询 like

like _(单个字符)

like %(任意长度的字符)
select * from tb_stu where name like '%ng'						//	查询name以ng结尾的所有结果
select * from tb_stu where name like '_ng'						//	查询name以ng结尾,并且ng前面只有一个字符的结果
select * from tb_stu where name like 'xiang'					//	查询name跟xiang相像的结果,没有使用任何通配符,类似于等值查询
select * from tb_stu where name like '___xiang'					//	查询name以xiang结尾,并且xiang之前由三个字符
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select * from tb_stu where name like '%ng';
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu where name like '_ng';
Empty set (0.00 sec)

mysql> select * from tb_stu where name like 'qiuxiang';
+------+----------+------+------+-------+------+
| sid  | name     | sex  | tel  | score | age  |
+------+----------+------+------+-------+------+
| 9528 | qiuxiang | girl | 110  |    80 |   18 |
| 9530 | qiuxiang | girl | 110  |    80 |   18 |
+------+----------+------+------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from tb_stu where name like 'xiang';
Empty set (0.00 sec)

mysql> select * from tb_stu where name like '___xiang';
+------+----------+------+------+-------+------+
| sid  | name     | sex  | tel  | score | age  |
+------+----------+------+------+-------+------+
| 9528 | qiuxiang | girl | 110  |    80 |   18 |
| 9530 | qiuxiang | girl | 110  |    80 |   18 |
+------+----------+------+------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from tb_stu where name like '____xiang';
+------+-----------+------+------+-------+------+
| sid  | name      | sex  | tel  | score | age  |
+------+-----------+------+------+-------+------+
| 9532 | chunxiang | NULL | NULL |  NULL | NULL |
+------+-----------+------+------+-------+------+
1 row in set (0.00 sec)

注意:

%代表任意长度字符

_代表任意的单个字符

没有任何通配符,即使使用like也与等值查询效果一致

  • 分支结构查询

基本语法:
case 
when 条件1 then 结果1
when 条件2 then 结果2
when 条件3 then 结果3
else 结果n
end
select sid, name, 
	case 
		when score >= 90 then 'A' 
		when score >= 80 then 'B'
		when score >= 70 then 'C'
		when score >= 60 then 'D'
		else 'E'
		end
from tb_stu;
select sid, name, 
	case 
		when score >= 90 then 'A' 
		when score >= 80 then 'B'
		when score >= 70 then 'C'
		when score >= 60 then 'D'
		else 'E'
		end as 'level'
from tb_stu;

时间查询

语法:

select 时间函数(参数列表)
执行时间函数,会自动生成一张虚拟表,一行一列
时间函数描述
now()当前系统时间(y M d h m s),不是所有的版本都支持
sysdate()当前系统时间(y M d h m s)
curdate()当前的日期
curtime()当前时间
week(date)当前日期是一年中的第几周
year(date)获取指定日期的年份
hour(date)获取指定时间的小时值
minute(date)获取指定时间的分钟值
adddate(date, n)计算指定date加上n天后的值,n也可以为负值
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-04-24 11:21:05 |
+---------------------+
1 row in set (0.00 sec)

mysql> select database();
+------------+
| database() |
+------------+
| saas       |
+------------+
1 row in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-04-24 11:36:28 |
+---------------------+
1 row in set (0.00 sec)

mysql> select sysdate();
+---------------------+
| sysdate()           |
+---------------------+
| 2024-04-24 11:38:30 |
+---------------------+
1 row in set (0.00 sec)

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2024-04-24 |
+------------+
1 row in set (0.00 sec)

mysql> select curtiem();
ERROR 1305 (42000): FUNCTION saas.curtiem does not exist
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 11:39:30  |
+-----------+
1 row in set (0.00 sec)

mysql> select week(now());
+-------------+
| week(now()) |
+-------------+
|          16 |
+-------------+
1 row in set (0.00 sec)

mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2024 |
+-------------+
1 row in set (0.00 sec)

mysql> select hour(now());
+-------------+
| hour(now()) |
+-------------+
|          11 |
+-------------+
1 row in set (0.00 sec)

mysql> select minute(now());
+---------------+
| minute(now()) |
+---------------+
|            42 |
+---------------+
1 row in set (0.00 sec)

mysql> select adddate(now(), 5)
 -> ;
+---------------------+
| adddate(now(), 5)   |
+---------------------+
| 2024-04-29 11:42:47 |
+---------------------+
1 row in set (0.00 sec)

mysql> select adddate(now(), 8);
+---------------------+
| adddate(now(), 8)   |
+---------------------+
| 2024-05-02 11:43:17 |
+---------------------+
1 row in set (0.00 sec)

mysql> select adddate(now(), -8);
+---------------------+
| adddate(now(), -8)  |
+---------------------+
| 2024-04-16 11:43:45 |
+---------------------+
1 row in set (0.00 sec)

字符串查询

语法:
select 字符串函数
字符串函数描述
concat(str1, str2, .. )将多个字符串拼接
insert(str, pos, len, newStr)将str中指定pos位置开始len个长度的内容替换为newStr
lower(str)将指定字符串转换为小写
upper(str)将指定的字符串转换为大写
substring(str, num, len)将str字符串指定num位置开始截取len个内容
mysql> select concat("hello", "world");
+--------------------------+
| concat("hello", "world") |
+--------------------------+
| helloworld               |
+--------------------------+
1 row in set (0.00 sec)

mysql> select concat("hello", "world", "saas");
+----------------------------------+
| concat("hello", "world", "saas") |
+----------------------------------+
| helloworldsaas                   |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select concat("My", "SQL");
+---------------------+
| concat("My", "SQL") |
+---------------------+
| MySQL               |
+---------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 0, "MySQL");
+-----------------------------------------+
| insert("这是一个数据库", 3, 0, "MySQL") |
+-----------------------------------------+
| 这是MySQL一个数据库                     |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 2, "MySQL");
+-----------------------------------------+
| insert("这是一个数据库", 3, 2, "MySQL") |
+-----------------------------------------+
| 这是MySQL数据库                         |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 2, "关系");
+----------------------------------------+
| insert("这是一个数据库", 3, 2, "关系") |
+----------------------------------------+
| 这是关系数据库                         |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 0, "关系");
+----------------------------------------+
| insert("这是一个数据库", 3, 0, "关系") |
+----------------------------------------+
| 这是关系一个数据库                     |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 5, 0, "关系");
+----------------------------------------+
| insert("这是一个数据库", 5, 0, "关系") |
+----------------------------------------+
| 这是一个关系数据库                     |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select lower("MySQL");
+----------------+
| lower("MySQL") |
+----------------+
| mysql          |
+----------------+
1 row in set (0.00 sec)

mysql> select upper("MySQL");
+----------------+
| upper("MySQL") |
+----------------+
| MYSQL          |
+----------------+
1 row in set (0.00 sec)

mysql> select substring("javaMySQLOracle", 5, 5)
 -> ;
+------------------------------------+
| substring("javaMySQLOracle", 5, 5) |
+------------------------------------+
| MySQL                              |
+------------------------------------+
1 row in set (0.00 sec)

聚合函数

语法:

select 聚合函数() from 表名
聚合函数描述
count()求总行数
max()求最大值
min()求最小值
sum()求和
avg()求平均值
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select count(*) from tb_stu;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

mysql> select count(sid) from tb_stu;
+------------+
| count(sid) |
+------------+
|          6 |
+------------+
1 row in set (0.00 sec)

mysql> select count(1) from tb_stu;
+----------+
| count(1) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select max(score) from tb_stu;
+------------+
| max(score) |
+------------+
|         99 |
+------------+
1 row in set (0.00 sec)

mysql> select min(score) from tb_stu;
+------------+
| min(score) |
+------------+
|         59 |
+------------+
1 row in set (0.00 sec)

mysql> select sum(score) from tb_stu;
+------------+
| sum(score) |
+------------+
|        395 |
+------------+
1 row in set (0.00 sec)

mysql> select avg(score) from tb_stu;
+------------+
| avg(score) |
+------------+
|         79 |
+------------+
1 row in set (0.00 sec)

注意:聚合函数自动忽略null值,所有的null将不进行统计

分组查询

语法:

select 列名 form表名 where 条件 group by 分组依据

group by根据指定分组依据进行分组

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select max(score) from tb_stu;
+------------+
| max(score) |
+------------+
|         99 |
+------------+
1 row in set (0.00 sec)

mysql> select max(score) from tb_stu group by sex;
+------------+
| max(score) |
+------------+
|       NULL |
|         99 |
|         80 |
+------------+
3 rows in set (0.00 sec)

mysql> select min(score) from tb_stu group by sex;
+------------+
| min(score) |
+------------+
|       NULL |
|         77 |
|         59 |
+------------+
3 rows in set (0.00 sec)

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.43-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use saas;
Database changed
mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
+------+--------------+------+------+-------+------+
3 rows in set (0.01 sec)

mysql> select sex from tb_stu;
+------+
| sex  |
+------+
| boy  |
| girl |
| girl |
+------+
3 rows in set (0.00 sec)

mysql> select distinct sex from tb_stu;
+------+
| sex  |
+------+
| boy  |
| girl |
+------+
2 rows in set (0.00 sec)

mysql> select name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
+--------------+------+
3 rows in set (0.00 sec)

mysql> select distinct name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
+--------------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> insert into tb_stu values(9530, 'qiuxiang', 'girl', 110, 80, 18);
Query OK, 1 row affected (0.01 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
| qiuxiang     | girl |
+--------------+------+
4 rows in set (0.00 sec)

mysql> select distinct name, sex from tb_stu;
+--------------+------+
| name         | sex  |
+--------------+------+
| zhouxingxing | boy  |
| qiuxiang     | girl |
| shiliu       | girl |
+--------------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu order by score;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu order by score asc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu order by score desc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> insert into tb_stu values(9531, 'zhuzhishan', 'boy', '112', 77, 22);
Query OK, 1 row affected (0.01 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu order by sex;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu order by sex asc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu order by sex desc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu order by sex desc, score;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu order by sex desc, score desc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu order by sex desc, score asc;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu where age = 30;
+------+--------+------+------+-------+------+
| sid  | name   | sex  | tel  | score | age  |
+------+--------+------+------+-------+------+
| 9529 | shiliu | girl | 114  |    59 |   30 |
+------+--------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where age = 30 and sex = 'girl';
+------+--------+------+------+-------+------+
| sid  | name   | sex  | tel  | score | age  |
+------+--------+------+------+-------+------+
| 9529 | shiliu | girl | 114  |    59 |   30 |
+------+--------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where age = 30 or sex = 'girl';
+------+----------+------+------+-------+------+
| sid  | name     | sex  | tel  | score | age  |
+------+----------+------+------+-------+------+
| 9528 | qiuxiang | girl | 110  |    80 |   18 |
| 9529 | shiliu   | girl | 114  |    59 |   30 |
| 9530 | qiuxiang | girl | 110  |    80 |   18 |
+------+----------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where not age = 30;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu where sex < 60;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set, 5 warnings (0.00 sec)

mysql> select * from tb_stu where score < 60;
+------+--------+------+------+-------+------+
| sid  | name   | sex  | tel  | score | age  |
+------+--------+------+------+-------+------+
| 9529 | shiliu | girl | 114  |    59 |   30 |
+------+--------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where score >= 60;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu where score = 100;
Empty set (0.00 sec)

mysql> select * from tb_stu where score != 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu where score <> 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu where score <= 100 and score >= 80;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 60 and 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 80 and 100;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 80 and 99;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from tb_stu where score between 180 and 99;
Empty set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> insert into tb_stu (sid, name) values(9532, 'chunxiang');
Query OK, 1 row affected (0.02 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select * from tb_stu where name is null;
Empty set (0.00 sec)

mysql> select * from tb_stu where sex is null;
+------+-----------+------+------+-------+------+
| sid  | name      | sex  | tel  | score | age  |
+------+-----------+------+------+-------+------+
| 9532 | chunxiang | NULL | NULL |  NULL | NULL |
+------+-----------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu where sex is not null;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
+------+--------------+------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select * from tb_stu where sid in (9527, 9528, 9599);
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
+------+--------------+------+------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select * from tb_stu where name like '%ng';
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from tb_stu where name like '_ng';
Empty set (0.00 sec)

mysql> select * from tb_stu where name like 'qiuxiang';
+------+----------+------+------+-------+------+
| sid  | name     | sex  | tel  | score | age  |
+------+----------+------+------+-------+------+
| 9528 | qiuxiang | girl | 110  |    80 |   18 |
| 9530 | qiuxiang | girl | 110  |    80 |   18 |
+------+----------+------+------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from tb_stu where name like 'xiang';
Empty set (0.00 sec)

mysql> select * from tb_stu where name like '___xiang';
+------+----------+------+------+-------+------+
| sid  | name     | sex  | tel  | score | age  |
+------+----------+------+------+-------+------+
| 9528 | qiuxiang | girl | 110  |    80 |   18 |
| 9530 | qiuxiang | girl | 110  |    80 |   18 |
+------+----------+------+------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from tb_stu where name like '____xiang';
+------+-----------+------+------+-------+------+
| sid  | name      | sex  | tel  | score | age  |
+------+-----------+------+------+-------+------+
| 9532 | chunxiang | NULL | NULL |  NULL | NULL |
+------+-----------+------+------+-------+------+
1 row in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select sid, name,
 ->  case
 ->          when score >= 90 then 'A'
 ->          when score >= 80 then 'B'
 ->          when score >= 70 then 'C'
 ->          when score >= 60 then 'D'
 ->          else 'E'
 ->          end
 -> from tb_stu;
+------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| sid  | name         | case
             when score >= 90 then 'A'
             when score >= 80 then 'B'
             when score >= 70 then 'C'
             when score >= 60 then 'D'
             else 'E'
             end |
+------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| 9527 | zhouxingxing | A
                                     |
| 9528 | qiuxiang     | B
                                     |
| 9529 | shiliu       | E
                                     |
| 9530 | qiuxiang     | B
                                     |
| 9531 | zhuzhishan   | C
                                     |
| 9532 | chunxiang    | E
                                     |
+------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------+
6 rows in set (0.00 sec)

mysql> select sid, name,
 ->  case
 ->          when score >= 90 then 'A'
 ->          when score >= 80 then 'B'
 ->          when score >= 70 then 'C'
 ->          when score >= 60 then 'D'
 ->          else 'E'
 ->          end as 'level'
 -> from tb_stu;
+------+--------------+-------+
| sid  | name         | level |
+------+--------------+-------+
| 9527 | zhouxingxing | A     |
| 9528 | qiuxiang     | B     |
| 9529 | shiliu       | E     |
| 9530 | qiuxiang     | B     |
| 9531 | zhuzhishan   | C     |
| 9532 | chunxiang    | E     |
+------+--------------+-------+
6 rows in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-04-24 11:21:05 |
+---------------------+
1 row in set (0.00 sec)

mysql> select database();
+------------+
| database() |
+------------+
| saas       |
+------------+
1 row in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-04-24 11:36:28 |
+---------------------+
1 row in set (0.00 sec)

mysql> select sysdate();
+---------------------+
| sysdate()           |
+---------------------+
| 2024-04-24 11:38:30 |
+---------------------+
1 row in set (0.00 sec)

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2024-04-24 |
+------------+
1 row in set (0.00 sec)

mysql> select curtiem();
ERROR 1305 (42000): FUNCTION saas.curtiem does not exist
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 11:39:30  |
+-----------+
1 row in set (0.00 sec)

mysql> select week();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
mysql> select week(now());
+-------------+
| week(now()) |
+-------------+
|          16 |
+-------------+
1 row in set (0.00 sec)

mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2024 |
+-------------+
1 row in set (0.00 sec)

mysql> select hour(now());
+-------------+
| hour(now()) |
+-------------+
|          11 |
+-------------+
1 row in set (0.00 sec)

mysql> select minute(now());
+---------------+
| minute(now()) |
+---------------+
|            42 |
+---------------+
1 row in set (0.00 sec)

mysql> select adddate(now(), 5)
 -> ;
+---------------------+
| adddate(now(), 5)   |
+---------------------+
| 2024-04-29 11:42:47 |
+---------------------+
1 row in set (0.00 sec)

mysql> select adddate(now(), 8);
+---------------------+
| adddate(now(), 8)   |
+---------------------+
| 2024-05-02 11:43:17 |
+---------------------+
1 row in set (0.00 sec)

mysql> select adddate(now(), -8);
+---------------------+
| adddate(now(), -8)  |
+---------------------+
| 2024-04-16 11:43:45 |
+---------------------+
1 row in set (0.00 sec)

mysql> select concat("hello", "world");
+--------------------------+
| concat("hello", "world") |
+--------------------------+
| helloworld               |
+--------------------------+
1 row in set (0.00 sec)

mysql> select concat("hello", "world", "saas");
+----------------------------------+
| concat("hello", "world", "saas") |
+----------------------------------+
| helloworldsaas                   |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select concat("My", "SQL");
+---------------------+
| concat("My", "SQL") |
+---------------------+
| MySQL               |
+---------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 0, "MySQL");
+-----------------------------------------+
| insert("这是一个数据库", 3, 0, "MySQL") |
+-----------------------------------------+
| 这是MySQL一个数据库                     |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 2, "MySQL");
+-----------------------------------------+
| insert("这是一个数据库", 3, 2, "MySQL") |
+-----------------------------------------+
| 这是MySQL数据库                         |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 2, "关系");
+----------------------------------------+
| insert("这是一个数据库", 3, 2, "关系") |
+----------------------------------------+
| 这是关系数据库                         |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 3, 0, "关系");
+----------------------------------------+
| insert("这是一个数据库", 3, 0, "关系") |
+----------------------------------------+
| 这是关系一个数据库                     |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select insert("这是一个数据库", 5, 0, "关系");
+----------------------------------------+
| insert("这是一个数据库", 5, 0, "关系") |
+----------------------------------------+
| 这是一个关系数据库                     |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select lower("MySQL");
+----------------+
| lower("MySQL") |
+----------------+
| mysql          |
+----------------+
1 row in set (0.00 sec)

mysql> select upper("MySQL");
+----------------+
| upper("MySQL") |
+----------------+
| MYSQL          |
+----------------+
1 row in set (0.00 sec)

mysql> select substring("javaMySQLOracle", 5, 5)
 -> ;
+------------------------------------+
| substring("javaMySQLOracle", 5, 5) |
+------------------------------------+
| MySQL                              |
+------------------------------------+
1 row in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select count(*) from tb_stu;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

mysql> select count(sid) from tb_stu;
+------------+
| count(sid) |
+------------+
|          6 |
+------------+
1 row in set (0.00 sec)

mysql> select count(1) from tb_stu;
+----------+
| count(1) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select max(score) from tb_stu;
+------------+
| max(score) |
+------------+
|         99 |
+------------+
1 row in set (0.00 sec)

mysql> select min(score) from tb_stu;
+------------+
| min(score) |
+------------+
|         59 |
+------------+
1 row in set (0.00 sec)

mysql> select sum(score) from tb_stu;
+------------+
| sum(score) |
+------------+
|        395 |
+------------+
1 row in set (0.00 sec)

mysql> select avg(score) from tb_stu;
+------------+
| avg(score) |
+------------+
|         79 |
+------------+
1 row in set (0.00 sec)

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

mysql> select max(score) from tb_stu;
+------------+
| max(score) |
+------------+
|         99 |
+------------+
1 row in set (0.00 sec)

mysql> select max(score) from tb_stu group by sex;
+------------+
| max(score) |
+------------+
|       NULL |
|         99 |
|         80 |
+------------+
3 rows in set (0.00 sec)

mysql> select min(score) from tb_stu group by sex;
+------------+
| min(score) |
+------------+
|       NULL |
|         77 |
|         59 |
+------------+
3 rows in set (0.00 sec)

任务:

创建一个学生表(student),包含以下字段,sid int,name varchar, sex char,score double,cid(班级编号) int

  1. 查询各个班级的 总人数

  2. 查询各个班级的平级分数

  3. 查询各个班级的成绩最高分

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值