2017-03-01 39 views
0

我有此表:列同時不能等於一定值(MySQL的)

+------+----------+-------------+ 
| id | category | category_id | 
+------+----------+-------------+ 
| 1 | 2  | 12   | 
| 2 | 1  | 12   | 
| 3 | 3  | 54   | 
| 4 | 3  | 17   | 
| 5 | 2  | 14   | 
+------+----------+-------------+ 

我NEAD得到以下:
當類別= 2:!CATEGORY_ID = 12

+------+----------+-------------+ 
| id | category | category_id | 
+------+----------+-------------+ 
| 2 | 1  | 12   | 
| 3 | 3  | 54   | 
| 4 | 3  | 17   | 
| 5 | 2  | 14   | 
+------+----------+-------------+ 

此代碼不起作用:

SELECT * FROM `table` WHERE `category`!=2 AND `category_id`!=12 
+0

在這個例子中輸出應該是什麼樣子? –

回答

0

我想你想要OR

select * 
from `table` 
where `category` != 2 
    or `category_id` != 12 

其中,按照De Morgan's law,是一樣的:

select * 
from `table` 
where not (
     `category` = 2 
     and `category_id` = 12 
     ) 

讀取 - 獲得除了當類別爲2的所有行和CATEGORY_ID是12一行

+0

非常感謝,你幫了我很多! –

相關問題