Mega Code Archive

 
Categories / MySQL / Select Clause
 

Explicit evaluation order

mysql> mysql> CREATE TABLE IF NOT EXISTS backpacks     -> (     ->   id INT AUTO_INCREMENT PRIMARY KEY,     ->   make VARCHAR(8),     ->   model VARCHAR(25),     ->   price DECIMAL(6,2)     -> ); Query OK, 0 rows affected (0.00 sec) mysql> mysql> # insert 4 records into the "backpacks" table mysql> INSERT INTO backpacks (make, model, price)   VALUES ("Adidas", "NYC Uptown", 17.99); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO backpacks (make, model, price)   VALUES ("Nike", "Arrow", 11.99); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO backpacks (make, model, price)   VALUES ("Nike", "Sevilla", 13.99); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO backpacks (make, model, price)   VALUES ("Reebok", "Streetsport", 11.99); Query OK, 1 row affected (0.00 sec) mysql> mysql> # show all data in the "backpacks" table mysql> SELECT * FROM backpacks; +----+--------+-------------+-------+ | id | make   | model       | price | +----+--------+-------------+-------+ |  1 | Adidas | NYC Uptown  | 17.99 | |  2 | Nike   | Arrow       | 11.99 | |  3 | Nike   | Sevilla     | 13.99 | |  4 | Reebok | Streetsport | 11.99 | +----+--------+-------------+-------+ 4 rows in set (0.00 sec) mysql> mysql> # show records where make is "Nike" or "Reebok" and price is 11.99 - without explicit evaluation order mysql> SELECT * FROM backpacks     -> WHERE make = "Nike" OR make = "Reebok" AND price = 11.99; +----+--------+-------------+-------+ | id | make   | model       | price | +----+--------+-------------+-------+ |  2 | Nike   | Arrow       | 11.99 | |  3 | Nike   | Sevilla     | 13.99 | |  4 | Reebok | Streetsport | 11.99 | +----+--------+-------------+-------+ 3 rows in set (0.00 sec) mysql> mysql> # show records where make is "Nike" or "Reebok" mysql> # and price is 11.99 - with explicit evaluation order mysql> SELECT * FROM backpacks     -> WHERE ( make = "Nike" OR make = "Reebok" )     -> AND price = 11.99; +----+--------+-------------+-------+ | id | make   | model       | price | +----+--------+-------------+-------+ |  2 | Nike   | Arrow       | 11.99 | |  4 | Reebok | Streetsport | 11.99 | +----+--------+-------------+-------+ 2 rows in set (0.00 sec) mysql> mysql> # delete this sample table mysql> DROP TABLE IF EXISTS backpacks; Query OK, 0 rows affected (0.00 sec)