使用WHERE [key]选择多行而不在MySQL中使用AND

时间:2014-11-20 09:38:27

标签: mysql

我有一张名为order_status的表格。

+-----------------+-------------+-------------------+
| order_status_id | language_id | name              |
+-----------------+-------------+-------------------+
|               2 |           1 | Processing        |
|               3 |           1 | Shipped           |
|               7 |           1 | Canceled          |
|               5 |           1 | Complete          |
|               8 |           1 | Denied            |
|               9 |           1 | Canceled Reversal |
|              10 |           1 | Failed            |
|              11 |           1 | Refunded          |
|              12 |           1 | Reversed          |
|              13 |           1 | Chargeback        |
|               1 |           1 | Pending           |
|              16 |           1 | Voided            |
|              15 |           1 | Processed         |
|              14 |           1 | Expired           |
|              17 |           1 | Confirmed         |
|              18 |           1 | Cash On Delivery  |
+-----------------+-------------+-------------------+

我目前正在处理一个查询,我可以选择多个order_status_id

通常你可以像这样选择多行:

SELECT FROM order_status WHERE order_status_id = 1 AND order_status_id = 2

上述命令会选择order_status_id 12

但是,我想知道你是否可以这样做:

SELECT FROM order_status WHERE order_status_id = (1,2),无需多次重写AND order_status_id = 2

尝试这一点,我收到了错误:ERROR 1241 (21000): Operand should contain 1 column(s)

我的问题是 - 如何在不使用WHERE的情况下多次使用AND选择多行?

1 个答案:

答案 0 :(得分:0)

使用in的正确方法是

WHERE order_status_id in (1,2)

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in