当前位置:网站首页>MySQL之CRUD
MySQL之CRUD
2022-07-05 14:45:00 【安离九歌】
目录
前言
上次分享了MySQL的账户管理,这次我们要分享的内容是CRUD,不管怎么样CRUD总要会点吧。
一、基本查询
#1.查询表中的单个字段
SELECT department_id FROM t_mysql_employees
#2.查询表中的多个字段
SELECT department_id,employee_id FROM t_mysql_employees
#3.查询表中的所有字段
SELECT * FROM t_mysql_employees
#4.查询常量值
SELECT 100;
SELECT 'john';
#5.查询表达式
SELECT 100%98;
#6.查询函数
SELECT VERSION();
#7.起别名
SELECT department_id 部门编号 FROM t_mysql_employees
#8.去重
SELECT DISTINCT department_id FROM t_mysql_employees
#案例:查询员工表中涉及到的所有的部门编号
SELECT DISTINCT department_id FROM t_mysql_employees
#9.+号的作用
SELECT 'join'+1
SELECT '100'+1
运行结果:
#1.查询表中的单个字段
#2.查询表中的多个字段
#3.查询表中的所有字段
#4.查询常量值
SELECT 100;
SELECT 'john';
#5.查询表达式
SELECT 100%98;
#6.查询函数
SELECT VERSION();
#7.起别名
#8.去重
#9.+号的作用
二、过滤查询
#一、按条件表达式筛选
#案例1:查询工资>12000的员工信息
SELECT * FROM t_mysql_employees WHERE salary > 12000
#案例2:查询部门编号不等于90号的员工名和部门编号
SELECT first_name,department_id FROM t_mysql_employees WHERE NOT(department_id=90)
#二、按逻辑表达式筛选
#案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
SELECT first_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 > 12000
#三、模糊查询
#1.like
#案例1:查询员工名中包含字符a的员工信息
SELECT * FROM t_mysql_employees WHERE first_name LIKE '%a%'
#案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
SELECT * FROM t_mysql_employees WHERE first_name LIKE '__e_a%'
#案例3:查询员工名中第二个字符为_的员工性
SELECT * FROM t_mysql_employees WHERE last_name LIKE
#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 first_name,job_id FROM t_mysql_employees WHERE job_id IN ('IT_PROG','AD_VP','AD_PRES')
#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 not commission_pct is null
#安全等于 <=>
#案例1:查询没有奖金的员工名和奖金率
SELECT first_name,commission_pct FROM t_mysql_employees WHERE commission_pct <=> null
#案例2:查询工资为12000的员工信息
SELECT * FROM t_mysql_employees WHERE salary <=>12000
#is null pk <=>
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
一、按条件表达式筛选
#案例1:查询工资>12000的员工信息
#案例2:查询部门编号不等于90号的员工名和部门编号
#二、按逻辑表达式筛选
#案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
#案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
#三、模糊查询
#1.like
#案例1:查询员工名中包含字符a的员工信息
#案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
#2.between and
#案例1:查询员工编号在100到120之间的员工信息#3.in
#案例:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
#4、is null
#案例1:查询没有奖金的员工名和奖金率
#案例2:查询有奖金的员工名和奖金率
#安全等于 <=>
#案例1:查询没有奖金的员工名和奖金率
#案例2:查询工资为12000的员工信息
#is null pk <=>
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
三、排序
order by 子句
#1、按单个字段排序
#案例:按员工表薪资排序
SELECT * FROM t_mysql_employees ORDER BY salary
#2、添加筛选条件再排序
#案例:查询部门编号>=90的员工信息,并按员工编号降序
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 em.salary*12*(1+IFNULL(commission_pct,0)),em.* FROM t_mysql_employees em ORDER BY em.salary*12*(1+IFNULL(commission_pct,0)) ASC
#5、按函数排序
#案例:查询员工名,并且按名字的长度降序
SELECT first_name FROM t_mysql_employees ORDER BY LENGTH(first_name) DESC
#6、按多个字段排序
#案例:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT * FROM t_mysql_employees ORDER BY salary DESC,employee_id ASC
#1.查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
SELECT em.first_name,em.department_id,em.salary*12*(1+IFNULL(commission_pct,0)) FROM t_mysql_employees em ORDER BY em.salary*12(1+IFNULL(commission_pct,0)) DESC,first_name asc
#2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
SELECT first_name,salary FROM t_mysql_employees WHERE NOT(salary BETWEEN 8000 AND 17000) ORDER BY salary desc
#3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT * FROM t_mysql_employees WHERE email LIKE '%e%' ORDER BY LENGTH(email)DESC,department_id ASC
order by 子句
#1、按单个字段排序
#案例:按员工表薪资排序
#2、添加筛选条件再排序
#案例:查询部门编号>=90的员工信息,并按员工编号降序
#3、按表达式排序
#案例:查询员工信息 按年薪降序
#4、按别名排序
#案例:查询员工信息 按年薪升序
#5、按函数排序
#案例:查询员工名,并且按名字的长度降序
#6、按多个字段排序
#案例:查询员工信息,要求先按工资降序,再按employee_id升序
#1.查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
#2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
#3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
四、案例
·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' , '女');
-- 教师表
insert into t_teacher values('01' , '张三');
insert into t_teacher values('02' , '李四');
insert into t_teacher values('03' , '王五');
-- 课程表
insert into t_course values('01' , '语文' , '02');
insert into t_course values('02' , '数学' , '01');
insert into t_course values('03' , '英语' , '03');
-- 成绩表
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);
#01)查询" 01 "课程比" 02"课程成绩高的学生的信息及课程分数
SELECT st.*,sc.score AS '01',sc2.score AS '02' FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid AND sc.cid='01'
LEFT JOIN t_score sc2 ON sc2.sid=st.sid AND sc2.cid='02'
WHERE sc.score>sc2.score
#02)查询同时存在" 01 "课程和" 02 "课程的情况
SELECT st.* FROM t_student st INNER JOIN t_score sc ON sc.sid=st.sid AND sc.cid='01'
WHERE st.sid IN (
SELECT st2.sid FROM t_student st2 INNER JOIN t_score sc2 ON sc2.sid=st2.sid AND sc2.cid='02'
)
#03)查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
SELECT st.* FROM t_student st INNER JOIN t_score sc ON sc.sid=st.sid AND sc.cid='01'
WHERE st.sid NOT IN (
SELECT st2.sid FROM t_student st2 INNER JOIN t_score sc2 ON sc2.sid=st2.sid AND sc2.cid='02'
)
#04)查询不存在" 01 "课程但存在" 02 "课程的情况
SELECT * FROM t_student st INNER JOIN t_score sc ON sc.sid=st.sid AND sc.cid='02'
WHERE st.sid NOT IN (
SELECT st2.sid FROM t_student st2 INNER JOIN t_score sc2 ON sc2.sid=st2.sid AND sc2.cid='01'
)
#05)查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT st.sid,st.sname,ROUND(AVG(sc.score),2) 平均分
FROM t_student st
LEFT JOIN t_score sc ON st.sid=sc.sid
GROUP BY st.sid HAVING AVG(sc.score)>=60;
#06)查询在t_score表存在成绩的学生信息
SELECT st.* FROM t_student st WHERE sid IN(
SELECT sc.sid FROM t_score sc
)
#07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
SELECT st.sid,st.sname,COUNT(sc.cid),ROUND(SUM(sc.score),2) AS '总成绩' FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid GROUP BY st.sid
#08)查询「李」姓老师的数量
SELECT COUNT(*) FROM t_teacher WHERE tname LIKE '李%'
#09)查询学过「张三」老师授课的同学的信息
SELECT st.* FROM t_student st WHERE sid IN(
SELECT sid FROM t_score WHERE cid = (
SELECT cid FROM t_course WHERE tid = (
SELECT tid FROM t_teacher WHERE tname = '张三'
)
)
)
#10)查询没有学全所有课程的同学的信息
SELECT st.*FROM t_student st WHERE sid NOT IN(
SELECT sid FROM t_score GROUP BY sid HAVING COUNT(cid)>=(SELECT COUNT(cid) FROM t_course)
)
#11)查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT st.sname FROM t_student st WHERE st.sid NOT IN (
SELECT sc.sid FROM t_score sc
INNER JOIN t_course c ON c.cid=sc.cid
INNER JOIN t_teacher t ON t.tid=c.tid AND t.tname="张三"
)
#12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT st.sid,st.sname,AVG(sc.score) FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid WHERE sc.sid IN(
SELECT sc.sid FROM t_score sc WHERE sc.score<60 OR sc.score IS NULL GROUP BY sc.sid HAVING COUNT(1)>=2
)
GROUP BY st.sid
#13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT st.*,sc.score FROM t_student st LEFT JOIN t_score sc ON sc.sid=st.sid WHERE sc.cid='01' AND sc.score<60 ORDER BY sc.score DESC ;
#14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT st.sid,st.sname,
(IFNULL((sc4.score),0)) "平均分",
(IFNULL((sc.score),0)) "语文",
(IFNULL((sc2.score),0)) "数学",
(IFNULL((sc3.score),0))"英语"
FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid AND sc.cid="01"
LEFT JOIN t_score sc2 ON sc2.sid=st.sid AND sc2.cid="02"
LEFT JOIN t_score sc3 ON sc3.sid=st.sid AND sc3.cid="03"
LEFT JOIN t_score sc4 ON sc4.sid=st.sid
GROUP BY st.sid
ORDER BY AVG(sc4.score) DESC
#15)查询各科成绩最高分、最低分和平均分:
#以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
#要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c.cid,c.cname,MAX(sc.score) "最高分",MIN(sc.score) "最低分",AVG(sc.score) "平均分"
,((SELECT COUNT(sid) FROM t_score WHERE score>=60 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) "及格率"
,((SELECT COUNT(sid) FROM t_score WHERE score>=70 AND score<80 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) "中等率"
,((SELECT COUNT(sid) FROM t_score WHERE score>=80 AND score<90 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) "优良率"
,((SELECT COUNT(sid) FROM t_score WHERE score>=90 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) "优秀率"
FROM t_course c
LEFT JOIN t_score sc ON sc.cid=c.cid
GROUP BY c.cid
1、
2、
3、
4、
5、
6、
7、
8、
9、
10、
边栏推荐
- Strong connection component
- [detailed explanation of Huawei machine test] happy weekend
- Thymeleaf common functions
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- Detailed explanation of usememo, memo, useref and other relevant hooks
- Fonctions communes de thymeleaf
- 【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
- 我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
- CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
- Mongdb learning notes
猜你喜欢
Coding devsecops helps financial enterprises run out of digital acceleration
[detailed explanation of Huawei machine test] character statistics and rearrangement
安装配置Jenkins
leetcode:881. 救生艇
申请代码签名证书时如何选择合适的证书品牌?
Topology visual drawing engine
基于TI DRV10970驱动直流无刷电机
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
Install and configure Jenkins
随机推荐
Share 20 strange JS expressions and see how many correct answers you can get
Thymeleaf common functions
两个BI开发,3000多张报表?如何做的到?
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Isn't it right to put money into the external market? How can we ensure safety?
Thymeleaf 常用函數
通过npm 或者 yarn安装依赖时 报错 出现乱码解决方式
【华为机试真题详解】欢乐的周末
Thymeleaf 常用函数
我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
Total amount analysis accounting method and potential method - allocation analysis
TS所有dom元素的类型声明
How can non-technical departments participate in Devops?
Explain Vue's plan to clean up keepalive cache in time
APR protocol and defense
Matrix chain multiplication dynamic programming example
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
微帧科技荣获全球云计算大会“云鼎奖”!
Want to ask the big guy, is there any synchronization from Tencent cloud Mysql to other places? Binlog saved by Tencent cloud MySQL on cos