MySQL学习足迹记录05--数据过滤--AND,OR,NOT,IN_MySQL

WBOY
发布: 2016-06-01 13:31:40
原创
868 人浏览过

bitsCN.com

MySQL学习足迹记录05--数据过滤--AND,OR,NOT,IN

 

1.AND操作符

   * 检索匹配满足所有给定条件的行

  

eg:   mysql> SELECT prod_id,prod_price,prod_name FROM products WHERE     -> vend_id =1003 AND prod_price<=10;+---------+------------+----------------+| prod_id | prod_price | prod_name      |+---------+------------+----------------+| FB      |      10.00 | Bird seed      || FC      |       2.50 | Carrots        || SLING   |       4.49 | Sling          || TNT1    |       2.50 | TNT (1 stick)  || TNT2    |      10.00 | TNT (5 sticks) |+---------+------------+----------------+5 rows in set (0.00 sec)
登录后复制

2.OR操作符

*检索匹配满足给定任一条件的行

 eg: mysql> SELECT prod_name,prod_price FROM products           -> WHERE vend_id=1002 or vend_id=1003;+----------------+------------+| prod_name      | prod_price |+----------------+------------+| Detonator      |      13.00 || Bird seed      |      10.00 || Carrots        |       2.50 || Fuses          |       3.42 || Oil can        |       8.99 || Safe           |      50.00 || Sling          |       4.49 || TNT (1 stick)  |       2.50 || TNT (5 sticks) |      10.00 |+----------------+------------+9 rows in set (0.01 sec)
登录后复制

3.计算次序

*WHERE可以包含任意数目的AND和OR操作符。

  eg:   mysql> SELECT prod_name,prod_price FROM products             -> WHERE vend_id=1002 OR vend_id=1003 AND prod_price>=10;                                     #在处理OR操作符之前,AND操作符会被优先处理+----------------+------------+| prod_name      | prod_price |+----------------+------------+| Detonator      |      13.00 || Bird seed      |      10.00 || Fuses          |       3.42 || Oil can        |       8.99 || Safe           |      50.00 || TNT (5 sticks) |      10.00 |+----------------+------------+6 rows in set (0.00 sec)
登录后复制

4.用()改变计算次序

eg:  mysql> SELECT prod_name,prod_price FROM products           -> WHERE (vend_id=1002 OR vend_id=1003) AND prod_price >= 10;                                       #先计算OR,再计算AND+----------------+------------+| prod_name      | prod_price |+----------------+------------+| Detonator      |      13.00 || Bird seed      |      10.00 || Safe           |      50.00 || TNT (5 sticks) |      10.00 |+----------------+------------+4 rows in set (0.00 sec)
登录后复制

5.IN操作符

*用来指定条件范围

eg:    mysql> SELECT prod_name,prod_price FROM products         -> WHERE vend_id IN (1002,1003)         -> ORDER BY prod_name;+----------------+------------+| prod_name      | prod_price |+----------------+------------+| Bird seed      |      10.00 || Carrots        |       2.50 || Detonator      |      13.00 || Fuses          |       3.42 || Oil can        |       8.99 || Safe           |      50.00 || Sling          |       4.49 || TNT (1 stick)  |       2.50 || TNT (5 sticks) |      10.00 |+----------------+------------+9 rows in set (0.00 sec)
登录后复制

上面的语句等效于:

mysql> SELECT prod_name,prod_price FROM products          -> WHERE vend_id =1002 OR vend_id = 1003         -> ORDER BY prod_name;+----------------+------------+| prod_name      | prod_price |+----------------+------------+| Bird seed      |      10.00 || Carrots        |       2.50 || Detonator      |      13.00 || Fuses          |       3.42 || Oil can        |       8.99 || Safe           |      50.00 || Sling          |       4.49 || TNT (1 stick)  |       2.50 || TNT (5 sticks) |      10.00 |+----------------+------------+9 rows in set (0.00 sec)
登录后复制

6.NOT操作符

*否定它之后所跟的任何条件

eg:   mysql> SELECT vend_id,prod_price FROM products            -> WHERE vend_id NOT IN (1002,1003)           -> ORDER BY prod_name;+---------+------------+| vend_id | prod_price |+---------+------------+|    1001 |       5.99 ||    1001 |       9.99 ||    1001 |      14.99 ||    1005 |      35.00 ||    1005 |      55.00 |+---------+------------+5 rows in set (0.00 sec)
登录后复制

 

 

bitsCN.com
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!