English | 正體中文 | 简体中文

SQL and or


范例资料表 : Orders (以下范例皆用此表当作资料来源)
OrderNo CustomerName Shipped SalePrice Quantity
20101213095 John True 13500 10
20100820135 Jimmy False 25096 22
20100609392 Vincent False 7200 5
20100530006 Tyler True 92500 50


Example
用AND选取2个不同条件的资料

Sytax : SELECT * FROM table_name WHERE search_condition1 AND search_condition2
SQL
SELECT * FROM Orders WHERE Shipped = true AND Quantity > 30

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20100530006 Tyler True 92500 50


Example
用OR选取2个不同条件的资料

Sytax : SELECT * FROM table_name WHERE search_condition1 OR search_condition2
SQL
SELECT * FROM Orders WHERE Shipped = true OR SalePrice > 10000

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20101213095 John True 13500 10
20100609392 Vincent False 7200 5
20100530006 Tyler True 92500 50


Example
用AND和OR来选取资料

Sytax : SELECT * FROM table_name WHERE (search_condition1 AND search_condition2) OR (search_condition3 AND search_condition4)
SQL
SELECT * FROM Orders WHERE (SalePrice > 8000 AND Quantity = 10) 
                                  OR (Shipped = false AND SalePrice = 7200)

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20101213095 John True 13500 10
20100609392 Vincent False 7200 5

© 2012 PureExample.com contact