MySQL ----- 过滤数据 where 子句(五)

 由于数据表中包含大量的数据,很少需要检索表中的所有的行,所以会指定搜索条件(search criteria)也称为过滤条件(filter condition),根据特定的操作将需要的数据从数据表中提取出来。

 where 子句简单使用:

注意:

  1、以表中的值作为查询条件的时候要注意这个值得类型,如char 字符形要放到单引号里面,uniqueidentifier也是要放到单引号里面

  2、在同时使用order by 子句和where 子句的时候,要将order by 子句放在where 后面,否则报错

   

-- 查询价格是10.00 的商品
select prod_name,prod_price from products where prod_price = 10.00;

注意:虽然数据也可以在应用层进行过滤,但是会导致通过查询检索出来的数据,有些是客户机应用(或开发语言)不需要的数据,需要客户机代码对返回的数据进行循环,提取需要的数据将极大的影响性能,而且通过网络将不需要的数据发送到客户机也会极大的浪费网络带宽,创建的应用程序也完全不具备可伸缩性,所以现在一般是通过sql 将需要的数据查询出来,在交给客户机应用。

where 字句操作符

 one、检查单个值

默认不区分大小写,SAFE 与Safe 匹配。

-- 查询名字是SAFE 的价格
select prod_name,prod_price from products where prod_name ='SAFE';

 two、不匹配检查

完整的表

 里面有两个10.00 的不想要

-- 查询价格不是10.00 的商品
select prod_id,prod_name,prod_price from products where prod_price<>10.00 order by prod_price;

 注意: 另一种方式

-- 查询价格不是10.00 的商品
select prod_id,prod_name,prod_price from products where prod_price !=10.00 order by prod_price;

three、范围值查询

使用where 操作符

1、  符号  数学中的大于、小于、不小于、不大于

-- 查询价格小于 10.00 的商品
 select prod_id,prod_name,prod_price from products where prod_price<10.00 order by prod_price;

2、空值检查

NULL(无值 no value):在创建的时候有一列是选择是否可以为空值(null),这个和字段中的  0 ,空字符串或仅仅包含空格是不同的。

-- 检查有null 的值
select * from customers where cust_email is null;

 3、使用between 操作符,要有两个值配合使查询即 开始值 and  结束值

注意:  开始的值和结束的值都包含

-- 查询价格在 4.49 到10 之间的商品

select prod_id,prod_name,prod_price from products where prod_price between 4.49 and 10 order by prod_price;

and 单独也是一个操作符,下一个讲

猜你喜欢

转载自www.cnblogs.com/obge/p/12936193.html