Day07-MySQL

DQL查询数据(重点)

where条件字句

作用:检索数据中符合条件的值

搜索的条件由一个或多个表达式组成,结果为布尔值

逻辑运算符

运算符 语法 描述
and && a and b a&&b 逻辑与,双真为真
or || a or b a||b 逻辑或,有真为真
not ! not a !a 逻辑非,取反
-- ============= WHERE ==============
SELECT `studentno`,`studentresult` FROM result

-- 查询考试成绩在 95-100分之间
SELECT `studentno`,`studentresult` FROM result
WHERE studentresult>=80 AND studentresult<=90

-- &&
SELECT `studentno`,`studentresult` FROM result
WHERE studentresult>=80 && studentresult<=90

-- 模糊查询(区间)
SELECT `studentno`,`studentresult` FROM result
WHERE studentresult between 80 AND 90

-- 除了1000号学生之外的同学的成绩
SELECT `studentno`,`studentresult` FROM result
WHERE NOT studentno =1000

-- !=
SELECT `studentno`,`studentresult` FROM result
WHERE studentno!=1000

模糊查询:比较运算符

运算符 语法 描述
IS NULL a is null 操作符为空,结果为真
IS NOT NULL a is not null 操作符不空,结果为真
BETWEEM a between b and c 如果a在b和c之间,结果为真
LIKE a like b SQL匹配,如果a匹配b,则结果为真
IN a in(a1,a2,…) 假设a在a1,a2,……其中一个值中,结果为真
-- ============= 模糊查询 ============= 
-- 查询姓刘的同学
-- like结合	%(代表任意字符)	_(一个字符)
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '刘%'

-- 查询姓刘的同学,名字后只有一个字的
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '刘_'

-- 查询名字中间有星字的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '%星%'

-- ============== IN ===============
-- IN只能匹配精确的字段值
-- 查询1001 1002 1003号学员信息 
SELECT `studentno`,`studentname` FROM `student`
WHERE studentno IN(1001,1002,1003)

-- 查询在上海浦东的学生
SELECT `studentno`,`studentname` FROM `student`
WHERE address IN('上海浦东')

-- ===== NULL    NOT NULL=====
-- 查询地址为空的学生
SELECT `studentno`,`studentname` FROM `student`
WHERE address IS NULL OR address=''

-- 查询有身份证号的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE identitycard IS NOT NULL 

-- 查询没有出生日期的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE `borndate` IS NULL

联表查询

JOIN 对比

![MySQL—7种join_tblemp表-CSDN博客]( https://img-blog.csdnimg.cn/20200727091407770.jpg?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpb2F5dTk2,size_16,color_FFFFFF,t_70)

-- ============联表查询 JOIN===========
--  查询参加了考试的同学(学号,姓名,科目编号,分数)
/*思路
1.分析需求,分析查询的字段来自哪些表
2.确定使用哪种连接查询
3.确定相同数据的交叉点
4.判断的条件:学生表中的 studentno=成绩表 studentno
*/

-- JOIN (连接的表) ON (判断的条件)  连接查询
-- WHERE                         等值查询

-- INNER JOIN
SELECT s.studentno,studentname,subjectno,studentresult 
FROM student AS s
INNER JOIN result AS r
WHERE s.studentno = r.studentno
-- WHERE用来连表后的过滤

-- LEFT JOIN
SELECT s.studentno,studentname,subjectno,studentresult 
FROM student AS s
LEFT JOIN result AS r
ON s.studentno = r.studentno
-- ON用来当连表前的条件

-- RIGHT JOIN
SELECT s.studentno,studentname,subjectno,studentresult 
FROM student AS s
RIGHT JOIN result AS r
ON s.studentno = r.studentno

-- 查询缺考的同学
SELECT s.studentno,studentname,subjectno,studentresult 
FROM student AS s
LEFT JOIN result AS r
ON s.studentno = r.studentno
WHERE studentresult IS NULL

--  查询参加了考试的同学(学号,姓名,科目名,分数)
SELECT s.studentno,studentname,subjectname,studentresult 
FROM student AS s
RIGHT JOIN result AS r
ON s.studentno = r.studentno
INNER JOIN subject AS sub
ON sub.subjectno = r.subjectno

-- 要查的数据 select …
-- 从哪几个表中查 from 表*** join 连接的表 on 交叉条件
-- 假设存在一种多张表查询,先查询两张然后增加
操作 描述
inner join 如果表中至少有一个匹配,就返回值
left join 会从左表中返回所有的值,即使右表中没有匹配
right join 会从右表中返回所有的值,即使左表中没有匹配
posted @ 2026-04-24 22:41  小空谜  阅读(18)  评论(0)    收藏  举报