当前位置:网站首页>MySql——CRUD
MySql——CRUD
2022-07-05 23:56:00 【lion tow】
目录
一、基础查询
1、语法:select 查询列表 from 表名
2、特点:
①、查询列表可以是:表中的字段、常量值、表达式、函数
②、查询的结果是一个虚拟的表格
3、 查询语句:
3.1查询表中的单个字段:
SELECT last_name FROM t_mysql_employees;
3.2:查询表中的多个字段:
SELECT last_name,salary,email FROM t_mysql_employees;
3.3:查询表中的所有字段:(企业多用方法一,因为效率更快)
方式一:
SELECT
`employee_id`,
`first_name`,
`last_name`,
`phone_number`,
`last_name`,
`job_id`,
`phone_number`,
`job_id`,
`salary`,
`commission_pct`,
`manager_id`,
`department_id`,
`hiredate`
FROM
t_mysql_employees ;
方式二:
SELECT * FROM t_mysql_employees;
3.4:查询常量值:
SELECT 100;
SELECT 'john';
结果:
3.5:查询常量值:
SELECT 100%98;
结果:
3.6:查询函数:
SELECT VERSION();
结果:
3.7:起别名目的:
①便于理解
②如果要查询的字段有重名的情况,使用别名可以区分开来
3.7.1:方式一:使用as:
SELECT 100%98 AS 结果;
SELECT salary AS "out put" FROM t_mysql_employees;
运行结果:
3.7.2:方式二:使用空格:
SELECT last_name 姓,first_name 名 FROM t_mysql_employees;
3.8:去重:把表格中重复的字段去掉
SELECT DISTINCT department_id FROM t_mysql_employees;
3.9:+号的作用:与JAVA中的+作对比
java中的+号:
①运算符,两个操作数都为数值型
②连接符,只要有一个操作数为字符串
mysql中的+号:只有唯一的功能:运算符
3.9.1+号的几种情况:
1、两个操作数都为数值型,则做加法运算:
select 100+90;
运行结果:
2、只要其中一方为字符型,将字符型数值转换成数值型,如果转换成功,则继续做加法运算:
select '123'+90
结果:
3、如果转换失败,则将字符型数值转换成0:
select 'john'+90;
结果:
4、只要其中一方为null,则结果肯定为null:
select null+10;
结果:
二、过滤和排序数据
1、按条件表达式筛选:
SELECT * FROM t_mysql_employees WHERE salary>12000;
2、按逻辑表达式筛选:
案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
SELECT last_name, salary, commission_pct FROM t_mysql_employees WHERE salary>=10000 AND salary<=20000;
3、模糊查询:
3.1、like:
案例2:查询员工名中包含字符a的员工信息
select * from employees where last_name like '%a%';
3.2、between and:
案例3:查询员工编号在100到120之间的员工信息
SELECT * FROM t_mysql_employees WHERE employee_id <= 120 AND employee_id>=100;
对比一下:
SELECT
*
FROM
t_mysql_employees
WHERE
employee_id BETWEEN 100 AND 120;
3.3、in:含义:判断某字段的值是否属于in列表中的某一项
案例4:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
SELECT
last_name,
job_id
FROM
t_mysql_employees
WHERE
job_id = 'IT_PROT' OR job_id = 'AD_VP' OR JOB_ID ='AD_PRES'
对比一下:
SELECT
last_name,
job_id
FROM
t_mysql_employees
WHERE
job_id IN( 'IT_PROT' ,'AD_VP','AD_PRES');
3.4、is null is not null:
案例5:查询没有奖金的员工名和奖金率; 查询有将近的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
t_mysql_employees
WHERE
commission_pct IS NULL;
SELECT
last_name,
commission_pct
FROM
t_mysql_employees
WHERE
commission_pct IS NOT NULL;
3.6安全等于 <=>
案例6:查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
t_mysql_employees
WHERE
commission_pct <=>NULL;
is null 和安全等于区别:
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
4、order by 子句:排序查询
语法:select 查询列表 from 表名【where 筛选条件】order by 排序的字段或表达式;
特点:
1、asc代表的是升序,可以省略desc代表的是降序
2、order by子句可以支持 单个字段、别名、表达式、函数、多个字段
3、order by子句在查询语句的最后面,除了limit子句
4.1按单个字段排序:
SELECT * FROM t_mysql_employees ORDER BY salary DESC;
4.2添加筛选条件再排序:
案例7:查询部门编号>=90的员工信息,并按员工编号降序
SELECT * FROM t_mysql_employees WHERE department_id>=90 ORDER BY employee_id DESC;
4.3按表达式排序:
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) FROM t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
4.4按表达式排序:
案例8:查询员工信息 按年薪升序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM t_mysql_employees ORDER BY 年薪 ASC;
4.5按函数排序:
案例9:查询员工名,并且按名字的长度降序
SELECT LENGTH(last_name),last_name FROM t_mysql_employees ORDER BY LENGTH(last_name) DESC;
4.6按函数排序:
案例10:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT * FROM t_mysql_employees ORDER BY salary DESC,employee_id ASC;
三、分组查询
功能:用作统计使用,又称为聚合函数或统计函数或组函数
分类: sum 求和、avg 平均值、max 最大值 、min 最小值 、count 计算个数
特点:1、sum、avg一般用于处理数值型 max、min、count可以处理任何类型
2、以上分组函数都忽略null值
3、可以和distinct搭配实现去重的运算
4、count函数的单独介绍一般使用count(*)用作统计行数
5、和分组函数一同查询的字段要求是group by后的字段
1、简单的使用:
SELECT SUM(salary) FROM t_mysql_employees;
SELECT AVG(salary) FROM t_mysql_employees;
SELECT MIN(salary) FROM t_mysql_employees;
SELECT MAX(salary) FROM t_mysql_employees;
SELECT COUNT(salary) FROM t_mysql_employees;
2、参数支持哪些类型:
SELECT SUM(last_name) ,AVG(last_name) FROM t_mysql_employees;
SELECT SUM(hiredate) ,AVG(hiredate) FROM t_mysql_employees;
SELECT MAX(last_name),MIN(last_name) FROM t_mysql_employees;
SELECT MAX(hiredate),MIN(hiredate) FROM t_mysql_employees;
SELECT COUNT(commission_pct) FROM t_mysql_employees;
SELECT COUNT(last_name) FROM t_mysql_employees;
3、是否忽略null:
SELECT SUM(commission_pct) ,AVG(commission_pct),SUM(commission_pct)/35,SUM(commission_pct)/107 FROM t_mysql_employees;
SELECT MAX(commission_pct) ,MIN(commission_pct) FROM t_mysql_employees;
SELECT COUNT(commission_pct) FROM t_mysql_employees;
SELECT commission_pct FROM t_mysql_employees;
4、和distinct搭配:
SELECT SUM(DISTINCT salary),SUM(salary) FROM t_mysql_employees;
SELECT COUNT(DISTINCT salary),COUNT(salary) FROM t_mysql_employees;
5、count函数的详细介绍:
SELECT COUNT(salary) FROM t_mysql_employees;
SELECT COUNT(*) FROM t_mysql_employees;
SELECT COUNT(1) FROM t_mysql_employees;
6、分组函数一同查询的字段有限制employee_id是最小的那个:
SELECT AVG(salary),employee_id FROM t_mysql_employees;
最终案例分析:
一、表结构要求:
-- 1.学生表-t_student
-- sid 学生编号,sname 学生姓名,sage 学生年龄,ssex 学生性别
-- 2.教师表-t_teacher
-- tid 教师编号,tname 教师名称
-- 3.课程表-t_course
-- cid 课程编号,cname 课程名称,tid 教师名称
-- 4.成绩表-t_score
-- sid 学生编号,cid 课程编号,score 成绩
-- 学生表
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 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;
边栏推荐
- Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
- Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
- 多普勒效應(多普勒頻移)
- "14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
- Which side projects can be achieved? Is it difficult for we media to earn more than 10000 a month?
- Breadth first search open turntable lock
- 激光slam学习记录
- After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
- 15 MySQL-存储过程与函数
- China Jinmao online electronic signature, accelerating the digitization of real estate business
猜你喜欢
随机推荐
[online chat] the original wechat applet can also reply to Facebook homepage messages!
【LeetCode】5. Valid palindrome
Spire. PDF for NET 8.7.2
Research notes I software engineering and calculation volume II (Chapter 1-7)
4点告诉你实时聊天与聊天机器人组合的优势
XML configuration file (DTD detailed explanation)
Spire Office 7.5.4 for NET
妙才周刊 - 8
[binary search tree] add, delete, modify and query function code implementation
Yunna | what are the main operating processes of the fixed assets management system
同事悄悄告诉我,飞书通知还能这样玩
Asynchronous task Whenall timeout - Async task WhenAll with timeout
What are Yunna's fixed asset management systems?
提升工作效率工具:SQL批量生成工具思想
【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
7.5模拟赛总结
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
QT -- thread
CloudCompare&PCL 点云随机添加噪声
如何获取localStorage中存储的所有值