一、基本查询
select * from t_mysql_employees;
desc t_mysql_employees;
insert into t_mysql_employees(first_name,last_name,email,phone_number,salary,commission_pct,manager_id,hiredate) values('li','si','28272712@qq.com','188273817392',3000,0.25,100,now());
select * from t_mysql_employees where phone_number='188273817392';
update t_mysql_employees set last_name='aa' where phone_number='188273817392';
delete from t_mysql_employees where phone_number='188273817392';
select now();
select 'zhangsan'+1;
select '100'+1;
插入后查询: 修改后查询: 删除后查询:
二、过滤查询
1、按条件表达式筛选
select * from t_mysql_employees where salary>12000;
运行效果:
select last_name,department_id from t_mysql_employees where not(department_id=90);
运行效果:
2、按逻辑表达式筛选
select last_name,salary,commission_pct from t_mysql_employees where salary between 10000 and 20000;
运行效果:
select * from t_mysql_employees where not(department_id between 90 and 110) or salary>15000;
运行效果:
3、模糊查询
select * from t_mysql_employees where last_name like '%a%'
运行效果:
select * from t_mysql_employees where last_name like '__e_a%';
select * from t_mysql_employees where last_name like '%e%a%';
运行效果:
select * from t_mysql_employees where last_name like '_$_%' escape '$';
运行效果:
select * from t_mysql_employees where employee_id between 100 and 120;
运行效果:
select * from t_mysql_employees where job_id in ('IT_PROG','AD_VP','AD_PRES');
运行效果:
select last_name,commission_pct from t_mysql_employees where commission_pct is not null;
select last_name,commission_pct from t_mysql_employees where commission_pct is 0.4;
select last_name,commission_pct from t_mysql_employees where commission_pct = null;
select last_name,commission_pct from t_mysql_employees where commission_pct <=> null;
?
?
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
运行效果:
三、排序
1、按单个字段排序
?select * from t_mysql_employees ORDER BY salary;
运行效果:
2、添加筛选条件再排序
?
?select * from t_mysql_employees where department_id>=90 ORDER BY employee_id desc;
运行效果:
3、按表达式排序
select em.salary*12*(1+IFNULL(commission_pct,0)),em.* from t_mysql_employees em ORDER BY em.salary*12*(1+IFNULL(commission_pct,0)) desc;
运行效果:
4、按函数排序
??select last_name,CONCAT(first_name,last_name),LENGTH(CONCAT(first_name,last_name)) from t_mysql_employees ORDER BY LENGTH(CONCAT(first_name,last_name)) desc;
运行效果:
5、按多个字段排序
?
select * from t_mysql_employees ORDER BY salary desc,employee_id asc;
运行效果:
四、案例
1、案例1
排序练习:
SELECT first_name,last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) from t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) desc,first_name asc,last_name asc;
运行效果:
select first_name,last_name,salary from t_mysql_employees where not(salary between 8000 and 17000) ORDER BY salary desc;
运行效果:
select * from t_mysql_employees em where email like '%e%' ORDER BY department_id asc,LENGTH(email) desc;
运行效果:
2、案例2
四个表:t_teacher,t_score,t_student,t_course 表数据:
insert into t_student values('01' , '赵雷' , '1990-01-01' , '男');
insert into t_student values('02' , '钱电' , '1990-12-21' , '男');
insert into t_student values('03' , '孙风' , '1990-12-20' , '男');
insert into t_student values('04' , '李云' , '1990-12-06' , '男');
insert into t_student values('05' , '周梅' , '1991-12-01' , '女');
insert into t_student values('06' , '吴兰' , '1992-01-01' , '女');
insert into t_student values('07' , '郑竹' , '1989-01-01' , '女');
insert into t_student values('09' , '张三' , '2017-12-20' , '女');
insert into t_student values('10' , '李四' , '2017-12-25' , '女');
insert into t_student values('11' , '李四' , '2012-06-06' , '女');
insert into t_student values('12' , '赵六' , '2013-06-13' , '女');
insert into t_student values('13' , '孙七' , '2014-06-01' , '女');
select * from t_student
insert into t_teacher values('01' , '张三');
insert into t_teacher values('02' , '李四');
insert into t_teacher values('03' , '王五');
select * from t_teacher
insert into t_course values('01' , '语文' , '02');
insert into t_course values('02' , '数学' , '01');
insert into t_course values('03' , '英语' , '03');
select * from t_course
insert into t_score values('01' , '01' , 80);
insert into t_score values('01' , '02' , 90);
insert into t_score values('01' , '03' , 99);
insert into t_score values('02' , '01' , 70);
insert into t_score values('02' , '02' , 60);
insert into t_score values('02' , '03' , 80);
insert into t_score values('03' , '01' , 80);
insert into t_score values('03' , '02' , 80);
insert into t_score values('03' , '03' , 80);
insert into t_score values('04' , '01' , 50);
insert into t_score values('04' , '02' , 30);
insert into t_score values('04' , '03' , 20);
insert into t_score values('05' , '01' , 76);
insert into t_score values('05' , '02' , 87);
insert into t_score values('06' , '01' , 31);
insert into t_score values('06' , '03' , 34);
insert into t_score values('07' , '02' , 89);
insert into t_score values('07' , '03' , 98);
select * from t_score
01)查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
select a.*,b.score,c.score from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02' and b.score>c.score
运行效果:
02)查询同时存在" 01 “课程和” 02 "课程的情况
select a.*,b.cid,c.cid from t_score b,t_student a,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02';
运行效果:
03)查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
select a.*,b.cid,c.cid from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid not in('02')
运行效果:
04)查询不存在" 01 “课程但存在” 02 "课程的情况
select a.*,b.cid,c.cid from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='02' and c.cid not in('01')
运行效果:
05)查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select a.sid,a.sname,AVG(b.score) from t_student a,t_score b where a.sid=b.sid GROUP BY b.sid HAVING AVG(b.score)>=60
运行效果:
06)查询在t_score表存在成绩的学生信息
select * from t_student where sid not in(select DISTINCT(sid) from t_score)
运行效果:
07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select a.sid,a.sname,COUNT(b.cid),SUM(b.score) from t_student a,t_score b where a.sid=b.sid GROUP BY b.sid
运行效果:
08)查询「李」姓老师的数量
select count(*) from t_teacher where tname like '李%'
运行效果:
09)查询学过「张三」老师授课的同学的信息
select a.*,c.tname from t_student a,t_score b,t_teacher c,t_course d where a.sid=b.sid and b.cid=d.cid and d.tid=c.tid and b.cid='01'
运行效果:
10)查询没有学全所有课程的同学的信息
select * from t_student where sid not in(select a.sid from t_student a,t_score b,t_score c,t_score d where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02' and d.cid='03')
运行效果:
11)查询没学过"张三"老师讲授的任一门课程的学生姓名
select sname from t_student where sname not in(select a.sname from t_student a,t_score b,t_teacher c,t_course d where a.sid=b.sid and b.cid=d.cid and d.tid=c.tid and b.cid='01')
运行效果:
12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select * from t_student a,t_score b where a.sid=b.sid and a.sid in(select c.sid from t_score c where c.score<60 GROUP BY c.sid HAVING count(*)>1)
运行效果:
13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select a.* from t_student a,t_score b where a.sid=b.sid and b.cid='01' and b.score<60 ORDER BY b.score desc
运行效果:
14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.*,AVG(b.score) from t_student a,t_score b where a.sid=b.sid GROUP BY b.sid HAVING AVG(b.score)>=0 ORDER BY AVG(b.score) desc
运行效果:
15)查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select a.cid,cname,max(a.score)'最高分',min(a.score)'最低分',AVG(a.score)'平均分',((select count(sid) from t_score where score>=60 and cid=b.cid)/(select count(sid) from t_score where cid=b.cid))'及格率' from t_score a inner join t_course b on a.cid=b.cid GROUP BY b.cid
运行效果:
|