mysql>SELECT true OR false AND false;+-------------------------+| true OR false AND false |+-------------------------+| 1 |+-------------------------+1rowinset
上面得出的结果,运算的过程是怎么样呢?
首先,MySQL对AND运算符求值,因此,false AND false返回false。
其次,MySQL对OR运算符求值,根据返回的false值再执行AND运算,因此true OR false返回true。
要更改评估/求值的顺序,请使用括号,例如:
SELECT (true OR false) AND false;
执行上面查询,得到如下结果 -
mysql>SELECT (true OR false) AND false;+---------------------------+| (true OR false) AND false |+---------------------------+| 0 |+---------------------------+1rowinset
上面得出的结果,运算的过程是怎么样呢?
首先,MySQL计算小括号中的表达式(true OR false)返回true
第二,MySQL评估求值语句的剩余部分,将上面第一步中计算出的表达式结果-true和剩余部分求值,即:true AND false返回false。
SELECT customername, countryFROM customersWHERE country ='USA'OR country ='France';
执行上面代码,得到如下结果 -
mysql>SELECT customername, country FROM customers WHERE country ='USA'OR country ='France';+------------------------------+---------+| customername | country |+------------------------------+---------+| Atelier graphique | France || Signal Gift Stores | USA || La Rochelle Gifts | France || Mini Gifts Distributors Ltd. | USA || Mini Wheels Co. | USA || Land of Toys Inc. | USA || Saveley & Henriot, Co. | France || Muscle Machine Inc | USA || Diecast Classics Inc. | USA || Technics Stores Inc. | USA |... ...+------------------------------+---------+48rowsinset
mysql> SELECT customername, country, creditLimit FROM customers WHERE (country = 'USA' OR country = 'France') AND creditlimit > 100000;
+------------------------------+---------+-------------+| customername | country | creditLimit |+------------------------------+---------+-------------+| La Rochelle Gifts | France | 118200 || Mini Gifts Distributors Ltd. | USA | 210500 || Land of Toys Inc. | USA | 114900 || Saveley & Henriot, Co. | France | 123900 || Muscle Machine Inc | USA | 138500 || Diecast Classics Inc. | USA | 100600 || Collectable Mini Designs Co. | USA | 105000 || Marta's Replicas Co. | USA | 123700 || Mini Classics | USA | 102700 || Corporate Gift Ideas Co. | USA | 105000 || Online Diecast Creations Co. | USA | 114200 |+------------------------------+---------+-------------+11 rows in set
请注意,如果不使用括号,查询将返回位于美国的客户或者位于法国并且信用额度大于10000的客户。
SELECT customername, country, creditLimitFROM customersWHERE country ='USA'OR country ='France'AND creditlimit >100000;
执行上面代码,得到如下结果(共 38 行) -
mysql> SELECT customername, country, creditLimit FROM customers WHERE country = 'USA' OR country = 'France' AND creditlimit > 100000;
+------------------------------+---------+-------------+| customername | country | creditLimit |+------------------------------+---------+-------------+| Signal Gift Stores | USA | 71800 || La Rochelle Gifts | France | 118200 || Mini Gifts Distributors Ltd. | USA | 210500 || Mini Wheels Co. | USA | 64600 || Land of Toys Inc. | USA | 114900 || Saveley & Henriot, Co. | France | 123900 || Muscle Machine Inc | USA | 138500 || Diecast Classics Inc. | USA | 100600 || Technics Stores Inc. | USA | 84600 || American Souvenirs Inc | USA | 0 || Cambridge Collectables Co. | USA | 43400 || Gift Depot Inc. | USA | 84300 || Vitachrome Inc. | USA | 76400 || Auto-Moto Classics Inc. | USA | 23000 || Online Mini Collectables | USA | 68700 || Toys4GrownUps.com | USA | 90700 || Boards & Toys Co. | USA | 11000 || Collectable Mini Designs Co. | USA | 105000 || Marta's Replicas Co. | USA | 123700 || Mini Classics | USA | 102700 || Mini Creations Ltd. | USA | 94500 || Corporate Gift Ideas Co. | USA | 105000 || Tekni Collectables Inc. | USA | 43000 || Classic Gift Ideas, Inc | USA | 81100 || Men 'R' US Retailers, Ltd. | USA | 57700 || Gifts4AllAges.com | USA | 41900 || Online Diecast Creations Co. | USA | 114200 || Collectables For Less Inc. | USA | 70700 || Classic Legends Inc. | USA | 67500 || Gift Ideas Corp. | USA | 49700 || The Sharp Gifts Warehouse | USA | 77600 || Super Scale Inc. | USA | 95400 || Microscale Inc. | USA | 39800 || FunGiftIdeas.com | USA | 85800 || West Coast Collectables Co. | USA | 55400 || Motor Mint Distributors Inc. | USA | 72600 || Signal Collectibles Ltd. | USA | 60300 || Diecast Collectables | USA | 85100 |+------------------------------+---------+-------------+38 rows in set