組合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)