《mysql 必知必会》整理1

来源:互联网 发布:wow.js下载 编辑:程序博客网 时间:2024/05/19 19:41

第6章:过滤数据

一、使用Where 字句

eg:

1、简单查询

      Select  prod_name , prod_price from products where prod_price = 2.5;

      Select  prod_name , prod_price from products where prod_name = fuses

2、范围值查询

     Select  prod_name , prod_price from products where prod_price  between 5 and 10;

注意:

①、同时使用order by 和where 字句时,order by 应位于where 之后

②、字符串要用引号,数字不用引号

③、Mysql 不区分大小写

④、where 后面可接:

=  等于

<> 不等于

!=不等于

< 小于

<= 小于等于

> 大于

>= 大于等于

between 在指定的两个值之间

* 3、空值检查

   Select  prod_name , prod_price from products where prod_price is null

注意:空置查询后,要检查下该数据结果是否真的符合


第7章:数据过滤

二、使用组合where字句

eg:

1、and且连接

      select prod_id , prod_price,prod_name from products where vend_id =1003 and prod_price <=10;

注意:①、可以多条件,每个条件之间以and连接

           ②、查询的结果所有条件都满足

2、or 或连接

     select prod_name , prod_price from products where vend_id =1002 or  vend_id =1003;

注意:查询的结果任意条件满足就行

3、圆括号

      ①、select  prod_name, prod_price from products where vend_id =1002 or vend_id =1003 and prod_price >=10;

      ②、select  prod_name, prod_price from products where (vend_id =1002 or vend_id =1003) and prod_price >=10;

注意:①、and的执行顺序or先,故以上两条sql语句执行结果不相同。若想要①中的or先执行,需加原括号。

           ②、通常同时使用and和or时,都应该使用圆括号明确的分组

4、in

      select prod_name.prod_price from products where vend_id in (1002,1003) order by prod_name;

注意:①、in(a,b)括号里面的值都可以进行匹配,每个条件用分隔

           ②、功能与or相当

5、not 

     select prod_name,prod_price from products where vend_id not in (1002,1003) order by prod_name;

注意:①、not in(a,b)匹配除括号里面的值

           ②、mysql支持使用not对in、between、exists字句取反


原创粉丝点击