mysql查操作(下)
select distinct sel_date as 选课日期
from records;
select distinct sel_date
from records
order by sel_date asc;
select distinct stu_addr as 学生籍贯
from students
where stu_addr is not null and trim(stu_addr) <> '';
select stu_name as 姓名
,stu_birth as 生日
from students
where stu_sex = 1
order by stu_birth asc;
select stu_name as 姓名
,stu_birth as 生日
from students
where stu_sex = 1
order by stu_birth asc,stu_id desc;
select stu_birth as 生日
,floor(datediff(curdate(),stu_birth)/365) as 年龄
from students
where stu_sex = 1
order by stu_birth;
select stu_name
,timestampdiff(year,stu_birth,curdate()) as 年龄
from students
where stu_sex = 1
order by stu_birth desc;
select min(stu_birth)
from students ;
select max(stu_birth)
from students;
select max(score)
from records
where cou_id = 1111;
select min(score)
from records
where stu_id = 1001;
select avg(score) as 平均分
,stddev_pop(score) as 标准差
,var_pop(score) as 方差
from records
where stu_id = 1001;
select avg(ifnull(score,0)) as 平均分
from records
where stu_id = 1001;
select avg(coalesce(score,0)) as 平均分
from records
where stu_id = 1001;
select sum(score)/ count(*) as 平均分
from records
where stu_id = 1001;
select case stu_sex when 1 then '男' when 0 then '女' else '总计' end as 性别
,count(*) as 人数
from students
group by stu_sex
with rollup;
select col_id
,count(*) as 人数
from students
group by col_id
with rollup;
select col_id
,case stu_sex when 1 then '男' else '女' end as 性别
,count(*) as 人数
from students
group by col_id,stu_sex
with rollup;
select stu_id
,round(avg(score),1)
from records
group by stu_id;
select stu_id as 学号
,round(avg(score),1) as 平均分
from records
group by stu_id
having avg(score) >= 90;
select stu_id as 学号
from records
group by stu_id
having min(score) > 80;
set @min_birth = (select min(stu_birth) from students);
select @min_birth;
select stu_name as 名字
from students
where stu_birth = @min_birth;
select @min_birth := (select min(stu_birth) from students);
select @min_birth;
select stu_name
from students
where stu_birth = @min_birth;
select stu_name
from students
where stu_birth = (select min(stu_birth)
from students);
set @sname = (select stu_id
from records
group by stu_id
having count(*) > 2);
select @sname;
select stu_name
from students
where stu_id in (@sname);
select stu_name
from students
where stu_id in (select stu_id
from records
group by stu_id
having count(*) > 2) ;
select stu_name
from students
where stu_id = any(select stu_id
from records
group by stu_id
having count(*) > 2) ;
select stu_name as 姓名
,stu_birth as 生日
,col_name as 学院
from students,colleges
where students.col_id = colleges.col_id;
select stu_name as 姓名
,stu_birth as 生日
,col_name as 学院
from students cross join colleges
where students.col_id = colleges.col_id;
select stu_name as 姓名
,stu_birth as 生日
,col_name as 学院
from students inner join colleges
on students.col_id = colleges.col_id;
select stu_name as 姓名
,stu_birth as 生日
,col_name as 学院
from students natural join colleges;
select stu_name as 姓名
,cou_name as 课程
,score as 分数
from students natural join records natural join courses
where score is not null;
select stu_name
,cou_name
,score
from students inner join records
inner join courses
on courses.cou_id = records.cou_id
and students.stu_id = records.stu_id
where score is not null;
select stu_name
,cou_name
,score
from students , courses, records
where students.stu_id = records.stu_id
and courses.cou_id = records.cou_id
and score is not null;
select stu_name as 姓名
,cou_name as 课程
,score as 分数
from students natural join records natural join courses
where score is not null
order by cou_id asc,score desc
limit 5;
select stu_name as 姓名
,cou_name as 课程
,score as 分数
from students natural join records natural join courses
where score is not null
order by cou_id asc,score desc
limit 5
offset 5;
select stu_name as 姓名
,cou_name as 课程
,score as 分数
from students natural join records natural join courses
where score is not null
order by cou_id asc,score desc
limit 5
offset 10;
select stu_name
,平均成绩
from students
natural join (select stu_id
,round(avg(score),1) as 平均成绩
from records
group by stu_id) as 临时表;
select stu_name
,选择数量
from students
natural join (select stu_id
,count(*) as 选择数量
from records
group by stu_id) as 临时表;
select stu_name as 姓名
,coalesce(选择数量,0)
from students
left outer join (select stu_id
,count(*) as 选择数量
from records
group by stu_id) as 临时表
on students.stu_id = 临时表.stu_id;
select stu_name as 姓名
,rec_id as 流水号
from students left outer join records
on students.stu_id = records.stu_id
where rec_id is null;