MYSQL必知必会-where语句

6章过滤数据

6.1使用WHERE子句

Select  nameprice  from  products  where  price  =  2.50;过滤出products表里price列等于2.50的列(nameprice两列)

6.2WHERE子句操作符

操作符

说明

=

等于

< >

不等于

=

不等于

<

小于

< =

小于等于

>

大于

> =

大于等于

BETWEEN

在指定的两个值之间

6.2.1检查单个值

Select  name, price   from  products  where  prod_name = ‘fuses’;name等于fuses,输出(name,price两个列,MySQL执行是不区分大小写)

Select  name, price   from  products  where  prod_price < ‘10’;price小于10的列,输出(name,price两个列,MySQL执行是不区分大小写)

Select  name, price   from  products  where  prod_price < = ‘10’;price小于等于10的列,输出(name,price两个列,MySQL执行是不区分大小写)

6.2.2不匹配检查

排除供应商编号为1003制造的所有产品

Select  id,  name  from  products  where  id < > 1003;

6.2.3范围值检查

  1. between操作符(必须有两个值,而且用and连接)

Select  name,  price  from products  where  price  between  5  and 10;price列取出510之间的name,price两列内容

#注:between操作符需要用两个值(低端值和高端值),切必须用and想连

6.2.4空值检查

检查表里name列为空的列

Select  id,  name,  price  from  products  where  name  is  null;

第七章数据过滤

7.1组合where子句

Mysql允许给出多个where子句,子句可以以两种方式使用;以and子句和or 子句的方式使用

7.1.1and操作符

取出表中id=1003price< = 10;的数据

Select  id,  name,  price,  from  products  where  id = 1003 and price < = 10;

7.1.2or操作符

取出表中id id 等于1002,或id 等于1003的数据

Select idname,  price  from products where id = 1002 or id = 1003;

7.1.3计算次序

价格为10元以上,且有10021003的所有产品

错误的SQL

Select  namepricefrom  produtes  where  id = 1002 or id = 1003 and price > = 10;

#因为优先级问题(and优先级大于or的优先级)

正确的SQL

#注:()的优先级大于and,and的优先级大于or的优先级

Select  namepricefrom  produtes  where  id = 1002 or id = 1003and price > = 10;

7.2什么是操作符?

用来联络或改变where子句中的子句的关键字,也称为逻辑操作符。

原文地址:https://www.cnblogs.com/A121/p/10457589.html