Mysql之CRUD

这篇博客详细介绍了MySQL数据库的CRUD操作,包括基本查询、过滤查询的案例、排序方法及实际应用示例,帮助读者掌握数据库操作技巧。
摘要由CSDN通过智能技术生成

目录

1.基本查询

2.​过滤查询案例

3.排序

4.案列


1.基本查询


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';

2.​过滤查询案例

	一、按条件表达式筛选
	#案例1:查询工资>12000的员工信息
	select * from t_mysql_employees where salary>12000;
	#案例2:查询部门编号不等于90号的员工名和部门编号
	select last_name,department_id from t_mysql_employees where not(department_id=90);
	
	#二、按逻辑表达式筛选
	#案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
	select last_name,salary,commission_pct from t_mysql_employees where salary between 10000 and 20000;
	#案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
	select * from t_mysql_employees where not(department_id between 90 and 110) or salary>15000;
	
	#三、模糊查询
	#1.like
	#案例1:查询员工名中包含字符a的员工信息
	select * from t_mysql_employees where last_name like '%a%'
	#案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
	select * from t_mysql_employees where last_name like '__e_a%';
	select * from t_mysql_employees where last_name like '%e%a%';
	#案例3:查询员工名中第二个字符为_的员工名
	select * from t_mysql_employees where last_name like '_$_%' escape '$';

3.排序

	#2.between and
	#案例1:查询员工编号在100到120之间的员工信息
	select * from t_mysql_employees where employee_id between 100 and 120;
	
	#3.in
	#案例:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
	select last_name,job_id from t_mysql_employees where job_id in ('IT_PROG','AD_VP','AD_PRES');
	#4、is null
	#案例1:查询没有奖金的员工名和奖金率
	#案例2:查询有奖金的员工名和奖金率
	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;

4.案列

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值