SQL多个AND和OR

时间:2015-09-09 04:33:36

标签: sql

任何人都可以帮助我理解使用AND的语法4次。

我有这段代码

WHERE (ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red') OR (Color = 'Black')

一旦评估,它会提供许多其他没有红色或黑色的列。请帮忙

3 个答案:

答案 0 :(得分:2)

WHERE (ListPrice BETWEEN 400 and 800) AND (Color in ('Red','Black'))

答案 1 :(得分:1)

您需要单独评估OR,然后AND评估其他条件。见这个

WHERE (ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red' OR Color = 'Black')

答案 2 :(得分:1)

您可以尝试以下

select * from t1 where pr >=400 and pr<=800 and (co='Red' or co='Black' );

以下是测试 -

create table t1 (pr number(10),co varchar2(10));

insert  into t1 values (700,'Red');
insert  into t1 values (500,'Black');
insert  into t1 values (200,'Black');
insert  into t1 values (900,'Red');
commit;

select * from t1 where pr >=400 and pr<=800 and (co='Red' or co='Black' );

输出 -

PR  CO

700 Red

500 Black