Mysql select 其中

Mysql select where

Mysql select 其中类型 1 的值 90 和 45 但不是 90 是我的示例 table

value   |   type    |   status
90          1           0
90          1           1
90          0           0
90          0           1
45          1           0
45          1           1

我的代码没有得到想要的输出

SELECT * FROM table WHERE type IN (1, 0) AND value != 90

期望的输出是:

value   |   type    |   status
90          0           0
90          0           1
45          1           0
45          1           1

您正在查看一个相当基本的复合条件 - 您想要 "all type=0" 或 "all type=1 except when value=90",可以表示为

WHERE (type = 0) OR (type = 1 AND value != 90)

(根据您自己的查询),或

WHERE (value = 45) OR (VALUE = 90 AND type != 1)

(根据你的第一句话。)

您可以尝试类似的操作:

create table test (val int, type int, status int);
insert into test values 
(90          ,1          ,0),
(90          ,1           ,1),
(90          ,0           ,0),
(90          ,0           ,1),
(45          ,1           ,0),
(45          ,1           ,1);

select *
from test 
where (val = 90 and type != 1) or val = 45;

| val | type | status |
|-----|------|--------|
|  90 |    0 |      0 |
|  90 |    0 |      1 |
|  45 |    1 |      0 |
|  45 |    1 |      1 |

SQLFiddle 示例:http://sqlfiddle.com/#!9/052c0/1