MySQL OR AND语法

时间:2014-10-05 18:07:41

标签: mysql syntax

刚开始学习MySQL这样一个noob问题和我在StackOverflow上的第一个问题。

假设我有12个订单状态,我想从5个中选择总数。我会用:

SELECT SUM(total) AS total FROM `orders` WHERE 
order_status_id = '1' OR 
order_status_id = '2' OR
order_status_id = '5' OR
order_status_id = '9' OR
order_status_id = '12';     

有没有更短的方法来获得这个?有点像...

SELECT SUM(total) AS total FROM `bd_order` WHERE
 order_status_id = ('1' OR '2' OR '5' OR '9' OR '12');

2 个答案:

答案 0 :(得分:2)

您可以使用IN

SELECT SUM(total) AS total 
FROM `bd_order` 
WHERE order_status_id in (1,2,5,9,12);

答案 1 :(得分:0)

将语句用括号括起来:

SELECT SUM(total) AS total FROM `orders` WHERE (
order_status_id = '1' OR 
order_status_id > '2' OR
order_status_id > '5' OR
order_status_id > '9' OR
order_status_id > '12'
); 

OR

SELECT SUM(total) AS total FROM `orders` WHERE order_status_id IN (1,2,5,9,12);