MySQL过滤数据操作方法梳理

2022-10-21 18:44:21
目录
前言1. where 字句的使用2. where 字句操作符2.1 检查单个值2.2 不匹配检查2.3 范围值检查2.4 空值检查3. 扩展3.1 SQL过滤与应用过滤3.2 引号的使用3.3 NULL的特殊性

前言

本实验中所用数据库创建SQL语句以及插入数据到数据库中的SQL语句链接:

链接:>

提取码: u3vs

书接上回说到,排序检索数据

1.>

在 select 语句中,数据会根据 where 字句指定的条件进行过滤,where 字句在from字句( 表名)之后给出。

示例: 选出价格为 3.49 的商品

select prod_name , prod_price
from Products
where prod_price = 3.49;

注意: order by 语句与 where 语句同时出现时,order by 要在where 字句后。(order by 字句的位置一定是 select 语句的最后一条字句)

示例:选出在 3~6 元之间的产品名和价格,并按价格排序

select prod_name, prod_price
from Products
where prod_price between 3 and 6
order by prod_price;

2.>

where 字句具有如下操作符 = 、< 、 > 、!= 、 <= 、 >= 、!< 、 !> 、between 、is null 等

2.1>

示例:

select prod_name, prod_price
from Products
where prod_price < 10;

2.2>

示例:

select vend_id , prod_name
from Products
where vend_id != 'DLL01';

2.3>

使用 between 运算符可以检索某个范围的值,它需要两个值表示一个范围。

示例:

select prod_name, prod_price
from Products
where prod_price between 5 and 10;

2.4>

示例:

select prod_name, prod_price
from Products
where prod_price is null;

3.>

3.1>

数据也可以在应用层进行过滤,即 select 语句返回所有数据,客户端代码对返回数据进行筛选,提取出自己需要的行。但是应用过滤具有以下缺点:

    影响应用性能所创建的应用不具备伸缩性服务器通过网络发送很多多余数据,浪费网络带宽

    3.2>

    当 where 字句筛选条件为 字符串时,需要用 引号限定字符串,用单引号和双引号都可以。而当筛选条件为数值时,不需要用引号。

    3.3>

    当我们通过 where 字句选择不包含指定值的行时,有时候我们希望返回 NULL 值的行,但是这不能实现。NULL 值比较特殊,进行匹配过滤和非匹配过滤时,都不会返回 NULL 值结果

    到此这篇关于MySQL过滤数据操作方法梳理的文章就介绍到这了,更多相关MySQL过滤数据内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!