3-SQL基础语法(检索、数据处理、过滤)

2020-04-07  本文已影响0人  一只森林鹿Luluzeng

一、数据检索

二、数据处理

三、数据过滤

四、举例练习

表例

1、查询来自安徽省和江苏省的学生,都属于哪个专业;字段:学号、姓名、籍贯、专业

select stu_id as 学号,name as 姓名,from_where as 籍贯,major as 专业
from student_info
where ((from_where ='安徽省') or ( from_where='江苏省'));

2、查询年龄不是20岁的学生;字段:学号、姓名、年龄

select stu_id as 学号,name as 姓名,age as 年龄
from student_info
where age !='20';

3、查询出生日期在1995-09-01到1995-10-31的学生信息;字段:学号、姓名、出生日期

select stu_id as 学号,name as 姓名,birth_date as 出生日期
from student_info
where birth_date between '1995-09-01'and '1995-10-31';

4、查询张姓学生的学生信息;字段:学号,姓名

select stu_id as 学号 ,name as 姓名
from student_info
where name like '张%';
select stu_id as 学号 ,name as 姓名
from student_info
where name regexp '^张';

5、查询姓名中含有宇的学生信息;字段:学号、姓名

select stu_id as 学号 ,name as 姓名
from student_info
where name like '%宇%';

6、查询姓名中包含宇的学生姓名,并去重;字段:姓名

select distinct name as 姓名
from student_info
where name like '%宇%';

7、查询stu_id=201600070,学院key=a的学生信息;字段:学号,姓名,专业。(将其学院改为理学院)

select stu_id as 学号,name as 姓名,college as 学院,JSON_EXTRACT(college,'$.a')
from student_info
where stu_id='2016100070';

--将其学院改为理学院

update student_info
 set major='理学院'
 where stu_id='2016100070';

8、查询from_where 为null和‘’的学生信息;字段:学号、姓名、籍贯。(将他们的籍贯改为安徽省)

select stu_id as 学号,name as 姓名,from_where as 籍贯
from student_info
where ((from_where ='')
or (from_where is null));

--将他们的籍贯改为安徽省

update student_info
set from_where='安徽省'
where ((from_where ='')
or (from_where is null));
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0

--检查

select stu_id as 学号,name as 姓名,from_where as 籍贯
from student_info
where stu_id = '2016100050'
or stu_id ='2016100060';
上一篇 下一篇

猜你喜欢

热点阅读