第四章:条件查询

一、where 字句

  1. 使用where对表中数据筛选,符合条件的数据,会出现
  2. select后面的* 或字段名,决定返回什么样的字段
  3. select中where子句,决定返回了什么记录

语法

sql 复制代码
select 字段1,字段2 from 表名 where 条件;

例子

sql 复制代码
/* 在students查询name=张三的记录 */ 
select * from students where name = '张三'; 

例2

sql 复制代码
/* 在students查询age等于26,姓名班级和老家 */
select name , class , hometown from students where  age =26; 

where后面支持多种运算符,进行条件的处理

  • 比较运算符
  • 逻辑运算符
  • 模糊查询
  • 范围查询
  • 空判断

二、比较运算符

  • 等于:=
  • 大于:>
  • 大于等于:>=
  • 小于:
  • 小于等于:
  • 不等于:!= 或 <>

语法

sql 复制代码
select 字段1,字段2 from 表名 where 条件;

例1

sql 复制代码
/* 查询表中age小于25的班级 */
select class from students where age < 25;

例2

sql 复制代码
/* 查询e表中age不等于25的记录 */
select * from students where age != 25;

=

三、逻辑运算符

and (和、与)

  1. 有两个条件

  2. 条件1 and 条件2

  3. 两个条件必须同时满足

例子

sql 复制代码
/* 查询age=26并且sex=男的记录 */
select * from students where age = 26 and sex = '男' ;

or(或)

  1. 有两个条件
  2. 条件1 or 条件2
  3. 两个条件满足一个即可

例子:

sql 复制代码
/* 查询age=26 或 name=张三 的记录 */
select * from students where age = 26 or name = '张三' ;

红色框圈起来的是满足了年龄,黑色框圈起来的是满足了姓名

四、not

  1. 条件成立,not以后就不成立;条件不成立,not以后就成立
sql 复制代码
-- 例子 --
-- 查询老家不在新疆的男生学生记录--
select * from students where  sex = '男' and not hometown = '新疆';

-- 查询老家不在新疆且不是男生学生记录--
select * from students where  not sex = '男' and not hometown = '新疆';

五、模糊查询

like

  • %表示任意多个任意字符
  • _表示任意一个字符
  • 字段名 like '字符%'
  • 指定字符开始,后面任意多个字符

例1

sql 复制代码
/* 查询name中以'郭'开头的学生记录 */
select * from students where name like '郭%';

例2

sql 复制代码
/* 查询name中以'李'开头且只有1个字的记录 */
select * from students where name like '李_';

例3

sql 复制代码
/* 查询name中包含苗的记录 */
select * from students where name like '%苗%';

例4

sql 复制代码
/* 查询姓李,且班级在2班的学生记录 */
select * from students where  name like '李%' and class = '2班';

六、范围查询

in(在)

sql 复制代码
语法
select * from 表名 where 条件 in(值,值,值)
  • 非连续性查找

例1

sql 复制代码
/* 查询家乡是上海、北京、新疆的学生记录 */
select * from students where hometown in ("上海","北京","新疆");

例2

sql 复制代码
/* 查询家乡是上海、北京、新疆的学生姓名和年龄,且性别是女生的记录 */
select name , age from students where hometown in ("上海","北京","新疆") and sex = '女';

between ... and..(在XXX和XXX之间)

  • 连续性查找
  • 包含开始值&结束值
sql 复制代码
语法
select * from 表名 where 条件 between ...and..;

例1

sql 复制代码
/* 查询年龄在24~35之间的学生记录 */
select * from students where age between 25 and 30;

例2

sql 复制代码
/* 查询年龄不在24~35之间的学生名字 */
select name from students where not age between 25 and 30;

七、空判断

  • 注意:null与"是不同的;
  • null不是0,也不是",在SQL里面代表空,什么也没有
  • null不能使用比较运算符
  • is null ---判断是否为null
  • is not null ---判断是否不为null

不能使用字段名 = 或 != null,这些都是错误的

例1

sql 复制代码
/* 查询学生身份证null的记录 */
select * from students where card is null;

例2

sql 复制代码
/* 插入一条数据 */
insert into students values (9,'王发发','女','广东',37,'4班','');

/* 查询学生身份证非null的记录 */
select * from students where card is  not null;

八、update&delete

update & delete 可以和where连用,和之前的语法一致

sql 复制代码
update
格式:set后面跟的是修改的记录 ,where 后面跟的是条件
update 表名 set 字段=值 where 条件;

例子:
修改age18,并且姓名为李四的学生,class 为4班
update students set class = '4班' where age =18 and name ='李四';
sql 复制代码
delete
格式2:删除指定字段值
delete from 表名 where 条件;

例子
删除class为2班并且姓名为郭小二的学生记录
delete from students where class = '2班' and name = '郭小二';

练习

sql 复制代码
/* 修改students表中,姓张的同学,班级为7班 */
update students set class = '7班' where name like '张%';

/* 删除students表中,年龄18~23中间和性别为男的学生记录 */
delete from students where (age between 18 and 23) and sex = '男';
相关推荐
LUCIAZZZ7 分钟前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding24 分钟前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
小Tomkk37 分钟前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台1 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go2 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
老纪的技术唠嗑局2 小时前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql
未来之窗软件服务3 小时前
JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟·东方仙盟数据库
寒山李白3 小时前
MySQL复杂SQL(多表联查/子查询)详细讲解
sql·mysql·子查询·多表联查
冰橙子id3 小时前
centos7编译安装LNMP架构
mysql·nginx·架构·centos·php
玛奇玛丶3 小时前
面试官:千万级订单表新增字段怎么弄?
后端·mysql