MySQL增删改查

目录

一,基本语法

      ①常用(

                1.增加

                2.删除

                3.修改

                4.查看

        )

       ②重要(

                1.按条件表达式筛选

                2.按逻辑表达式筛选

                3.模糊查询

                4.is  null

                5.安全等于 <=>

                6.order by

                7.取别名

                8.去重

                9.‘+’ 加号的作用

         ) 

二,案例


一,基本语法

      ①常用(

                1.增加

语法:insert into 表名 values (内容,内容,....)        单个增加并且全部列名都要有

          insert into 表名  (列名,列名,...) values(内容,内容,....)        单个增加选择性加入内容

     

选择性插入内容: 

 

未插入内容的字段显示为空: 

                2.删除

语法:delete from 表名 where 条件语句             删除表中数据   

           drop table  表名                                         删除整张表   

        

运行了删除语句:

已经删除:‘

drop  delete from 表名: 

注意表:

执行sql语句:

此时表已经被删除:

                3.修改

语法:update  表名  set   要进行修改的字段   where  条件语句

执行修改的sql语句

效果

                4.查看

语法:select  * from 表名

        )

       ②重要(

                1.按条件表达式筛选

    一、按条件表达式筛选
    #案例1:查询工资>12000的员工信息
    select * from t_mysql_employees where salary>12000
    #案例2:查询部门编号不等于90号的员工名和部门编号
    ​select * from t_mysql_employees where department_id <> 90
    
    select * from t_mysql_employees where not (department_id=90)
 

                2.按逻辑表达式筛选

between ...  and  ....

    #二、按逻辑表达式筛选
    #案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
    select * 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
 

                3.模糊查询

    #三、模糊查询
    #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%'
    #案例3:查询员工名中第二个字符为_的员工名
    select * from t_mysql_employees where last_name like '_$_%' escape '$'
    #2.between and
    #案例1:查询员工编号在100到120之间的员工信息
  select * from t_mysql_employees where department_id BETWEEN 100 and 120
    #3.in
    #案例:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
    select * from t_mysql_employees where job_id in('IT_PROG','AD_VP','AD_PRES')

 IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
    <=>   :既可以判断NULL值,又可以判断普通的数值,可读性较低

                4.is  null

    #4、is null
    #案例1:查询没有奖金的员工名和奖金率
    select first_name,commission_pct from t_mysql_employees where commission_pct is null
    #案例2:查询有奖金的员工名和奖金率
    
  select first_name,commission_pct from t_mysql_employees where  commission_pct  is not null
 

                5.安全等于 <=>

    #安全等于 <=>
    ​#案例1:查询没有奖金的员工名和奖金率
    select first_name,commission_pct from t_mysql_employees where  commission_pct <=> null

    #案例2:查询工资为12000的员工信息
    ​select * from t_mysql_employees where  salary <=> 12000
 

                6.order by

​#1、按单个字段排序
    #案例:按员工表薪资排序
    ​select * from t_mysql_employees ORDER BY salary desc
    #2、添加筛选条件再排序
    ​#案例:查询部门编号>=90的员工信息,并按员工编号降序
    
    select * from (select * from t_mysql_employees where department_id >= 90 )a ORDER BY manager_id desc
 

                7.取别名

    #7.起别名
    select employee_id as 员工编号 from t_mysql_employees
 

                8.去重

DISTINCT

    #8.去重
    select DISTINCT manager_id from t_mysql_employees
 

                9.‘+’ 加号的作用

 ①运算符,两个操作数都为数值型
 ②连接符,只要有一个操作数为字符串

    +号的作用
    java中的+号:
    ①运算符,两个操作数都为数值型
    ②连接符,只要有一个操作数为字符串
    ​mysql中的+号:
    仅仅只有一个功能:运算符
    select 'liuliu'+1

运行结果为1 

 运行结果为相加之和:

练习:

排序练习
    #1.查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
    SELECT last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) 年薪
    FROM t_mysql_employees
    ORDER BY 年薪 DESC,last_name ASC
    
    #2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
    
    SELECT last_name,salary FROM t_mysql_employees WHERE salary NOT BETWEEN 8000 AND 17000 ORDER BY salary DESC
    
    #3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
​    SELECT *,LENGTH(email) FROM t_mysql_employees WHERE email LIKE '%e%' ORDER BY LENGTH(email) DESC,department_id ASC

         ) 

二,案例

--一、表结构要求:

-- 1.学生表-t_student
-- sid 学生编号,sname 学生姓名,sage 学生年龄,ssex 学生性别
create table t_student(
	sid VARCHAR(10) PRIMARY key,
	sname VARCHAR(20),
	sage datetime,
	ssex VARCHAR(10)
)


-- 2.教师表-t_teacher
-- tid 教师编号,tname 教师名称
create table t_teacher(
	tid VARCHAR(10) PRIMARY KEY,
	tname VARCHAR(10)
)


-- 3.课程表-t_course
-- cid 课程编号,cname 课程名称,tid 教师名称
create table t_course(
		cid VARCHAR(10) PRIMARY KEY,
		cname VARCHAR(10),
		tid  VARCHAR(10)
)



-- 4.成绩表-t_score
-- sid 学生编号,cid 课程编号,score 成绩

create table t_score(
		sid VARCHAR(10) ,
		cid VARCHAR(10),
		score int
)



--三、表数据:

-- 学生表
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_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' 

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 c.cid not in('01') and b.cid='02' 

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;



查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 

 查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值