组合where子句以建立功能更加强大的更高级的搜索条件,并使用not和in操作符。
一、组合where子句
MySQL允许给出多个where子句,这些子句可以使用:以and子句的方式或or子句的方式使用。
1.1 and操作符
select prod_id, prod_price, prod_name from products where vend_id = 1003 and prod_price <=10;
1.2 or操作符
select vend_id, prod_price, prod_name from products where vend_id = 1003 or vend_id = 1002;
1.3 计算次序
在where子句中使用圆括号,任何时候使用具有and和or操作符的where子句,都应该使用圆括号明确地分组操作符,不要过分依赖默认计算次序,(and操作符优先级高于or)。
select vend_id, prod_price, prod_name from products where vend_id = 1002 or vend_id = 1003 and prod_price >=10;(会查询出vend_id为1002但是prod_price小于10的数据)
select vend_id, prod_price, prod_name from products where (vend_id = 1002 or vend_id = 1003) and prod_price >=10;
二、in操作符
select vend_id, prod_price, prod_name from products where vend_id in (1002, 1003)
三、not操作符
select vend_id, prod_price, prod_name from products where vend_id not in (1002, 1003)