MySQL 查询题型总结
use `school`;
select * from tb_student;
select stu_id,
stu_name,
stu_addr
from tb_student;
use school;
select stu_id as 学号,
stu_name as 姓名,
stu_addr as 籍贯
from tb_student;
select stu_id as '学生编号',
stu_name as '学生姓名'
from tb_student;
select stu_name,
stu_birth
from tb_student
where stu_sex = 0;
select stu_name,
stu_birth
from tb_student
where stu_sex = 0 and
stu_addr = '四川成都';
select stu_name,
stu_birth
from tb_student
where stu_sex = 0 or
stu_addr = '四川成都';
select stu_name,
stu_sex,
stu_birth
from tb_student
where stu_birth>='1980-1-1' and
stu_birth<='1990-1-1';
select stu_name,
stu_sex +10,
stu_birth
from tb_student
where stu_birth between '1980-1-1' and '1990-1-1';
select cou_name as '课程名称',
cou_credit as '课程学分'
from tb_course
where cou_credit > 2;
select cou_name ,
cou_credit
from tb_course
where cou_credit mod 2 <> 0;
select stu_id
from tb_record
where cou_id = 1111 and
score > 90;
select stu_name as '学生姓名',
case stu_sex when 1 then '男' when 0 then '女' else '未知' end as '学生性别'
from tb_student
where stu_name = '杨过';
select stu_name as '学生性别',
case stu_sex when 1 then '男' when 0 then '女' else '未知' end as '性别'
from tb_student
where stu_name = '杨过';
select stu_name,
stu_sex
from tb_student
where stu_name like '杨_' or
stu_name like '杨__';
select stu_name,
case stu_sex when 1 then '男' when 0 then '女' else '未知' end as '性别'
from tb_student
where stu_name like '杨%';
select stu_name,
stu_sex
from tb_student
where stu_name like '杨_';
select stu_name,
stu_sex
from tb_student
where stu_name like '杨__';
select stu_name,
stu_sex
from tb_student
where stu_name like '%不%' or
stu_name like '%嫣%';
select stu_name,
stu_sex
from tb_student
where stu_name like '%不%'
union
select stu_name,
stu_sex
from tb_student
where stu_name like '%嫣%';
select stu_name
from tb_student
where stu_name regexp '[杨][\\u4e00-\\u9fa5]{1,2}';
select stu_name
from tb_student
where stu_name regexp '[杨林][\\u4e00-\\u9fa5]{2}';
select stu_name
from tb_student
where stu_addr is null ;
update tb_student set stu_addr = '' where stu_id = 3755;
select stu_name
from tb_student
where stu_addr is not null ;
select stu_name
from tb_student
where stu_addr is null or
stu_addr = '';
select stu_name
from tb_student
where stu_addr is not null and
stu_addr <> '';
update tb_student set stu_addr = ' ' where stu_id = 3755;
select stu_name
from tb_student
where stu_addr is null or
trim(stu_addr) = '';
select stu_name
from tb_student
where stu_addr is not null and
trim(stu_addr) <> '';
select distinct sel_date
from tb_record;
select distinct sel_date
from tb_record
where stu_addr is not null and
trim(stu_addr) <>'';
select stu_name,
stu_birth
from tb_student
where stu_sex = 1
order by stu_birth asc, stu_id desc;
select now();
select curdate();
select curtime();
select stu_birth,
stu_name,
floor(datediff(curdate(),stu_birth)/365) as '年龄'
from tb_student
where stu_sex = 1 order by 年龄 desc;
help functions;
help time;
select min(stu_birth)
from tb_student;
select max(stu_birth)
from tb_student;
select max(score)
from tb_record
where cou_id = 1111;
select min(score)
from tb_record
where stu_id = 1001;
select round(avg(score),1)
from tb_record
where stu_id = 1001;
select round(avg(score),1)
from tb_record
where stu_id = 1001;
select sum(score)/count(*)
from tb_record
where stu_id = 1001;
select avg(ifnull(score,0))
from tb_record
where stu_id = 1001;
select avg(coalesce(score,0))
from tb_record
where stu_id = 1001;
select avg(case when score is null then 0 else score end)
from tb_record
where stu_id = 1001;
select variance(score)
from tb_record
where stu_id = 1001;
select round(stddev(score),2)
from tb_record
where stu_id = 1001;
select round(stddev(score),2)
from tb_record
where stu_id = 1002;
select stddev_samp(score)
from tb_record
where stu_id = 1002;
select var_samp(score)
from tb_record
where stu_id = 1002;
select stddev_pop(score)
from tb_record
where stu_id = 1002;
select var_pop(score)
from tb_record
where stu_id = 1002;
help functions;
help 'Aggregate Functions and Modifiers';
select case stu_sex when 1 then '男'else '女' end as 性别,
count(*) as 人数
from tb_student
group by stu_sex;
select col_id as 学院,
count(*) as 人数
from tb_student
group by col_id;
select col_id as 学院编号,
case stu_sex when 1 then '男' else '女' end as 性别,
count(*) as 人数
from tb_student
group by col_id,stu_sex;
select stu_id as 学生学号,
round(avg(score),1) as 平均分
from tb_record
group by stu_id;
select stu_id,
avg(score) as '平均成绩'
from tb_record
group by stu_id
having avg(score) >= 90;
select stu_id as 学号,
cou_id as 课程编号,
round(avg(score),1) as 平均成绩
from tb_record
where cou_id in (1111,2222,3333)
group by stu_id
having avg(score) >= 90;
select min(stu_birth)
from tb_student;
select @x := min(stu_birth)
from tb_student;
select @x ;
select stu_name
from tb_student
where stu_birth = @x;
select stu_name
from tb_student
where stu_birth = (select min(stu_birth)
from tb_student);
select stu_name
from tb_student
where stu_id in (select stu_id
from tb_record
group by stu_id
having count(*) > 2);
select stu_name
from tb_student
where stu_id = any (select stu_id
from tb_record
group by stu_id
having count(*) > 2);
%*
杨过
王语嫣
项少龙
*%
select stu_name,
stu_birth,
col_name
from tb_student as t1,tb_college as t2
where tb_student.col_id = tb_college.col_id;
select stu_name,
stu_birth,
col_name
from tb_student inner join tb_college on tb_student.col_id = tb_college.col_id ;
select stu_name,
cou_name,
score
from tb_student as t1,tb_course as t2,tb_record as t3
where t1.stu_id = t3.stu_id and
t2.cou_id = t3.cou_id and
score is not null
select stu_name,
cou_name,
score
from tb_student inner join tb_record on tb_student.stu_id = tb_record.stu_id inner join tb_course on tb_course.cou_id = tb_record.cou_id
where score is not null ;
select stu_name,
cou_name,
score
from tb_student natural join tb_course natural join tb_record
where score is not null;
select stu_name,
cou_name,
score
from tb_student natural join tb_course natural join tb_record
where score is not null
order by cou_id asc ,score desc
limit 5
offset 10;
select stu_name,
cou_name,
score
from tb_student natural join tb_course natural join tb_record
where score is not null
order by cou_id asc ,score desc
limit 10 , 5;
use school;
select stu_name,
cou_name,
score
from tb_student natural join tb_course natural join tb_record
where score is not null
order by cou_id asc ,score desc
limit 5 , 5;
select stu_name,
cou_name,
score
from tb_student natural join tb_course natural join tb_record
where score is not null
order by cou_id asc ,score desc
limit 10 , 5;
select stu_name ,
avg_score
from tb_student natural join (select stu_id,
round(avg(score),1) as avg_score
from tb_record
group by stu_id) as tb_ave;
select stu_name as 学生姓名,
round(avg(score),1) as 平均成绩
from tb_student natural join tb_record
group by stu_id;
select stu_name as 学生姓名,
count(*) as 选课数量
from tb_student natural join tb_record
group by stu_id;
select stu_name as 学生姓名,
count as 选课数量
from tb_student inner join select(
group by stu_id;