Maison > base de données > tutoriel mysql > le corps du texte

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

WBOY
Libérer: 2016-06-01 13:31:40
original
868 Les gens l'ont consulté

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)
Copier après la connexion

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)
Copier après la connexion

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)
Copier après la connexion

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)
Copier après la connexion

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)
Copier après la connexion

上面的语句等效于:

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)
Copier après la connexion

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)
Copier après la connexion

 

 

bitsCN.com
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!