SQL多重与或
有谁能帮我理解一下使用和的语法吗 我有这段代码SQL多重与或,sql,Sql,有谁能帮我理解一下使用和的语法吗 我有这段代码 WHERE (ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red') OR (Color = 'Black') WHERE (ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red' OR Color = 'Black') 其中(ListPrice>=400)和(ListPrice也许您应该将其
WHERE (ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red') OR (Color = 'Black')
WHERE (ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red' OR Color = 'Black')
其中(ListPrice>=400)和(ListPrice也许您应该将其更改为
WHERE ((ListPrice >= 400) AND (ListPrice <= 800)) AND (Color = 'Red' OR Color = 'Black')
其中((ListPrice>=400)和(ListPrice您需要分别评估或,然后将和与其他条件一起评估。请参见此
其中(ListPrice>=400)和(ListPrice您可以像下面这样尝试
select * from t1 where pr >=400 and pr<=800 and (co='Red' or co='Black' );
括号很重要
例如:
WHERE ((ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red')) OR (Color = 'Black')
其中((ListPrice>=400)和(ListPrice其中(ListPrice>=400)和(ListPrice从mytable中选择*),其中(ListPrice>=400)和(ListPrice我应该使用运算符,而不是中间和中间。这就是为什么我选择使用>=,原因是和
优先于或
,这就是为什么您需要添加括号。哦,您可以将其替换为((ListPrice>=400)和(ListPrice@winter只需更新您的答案以满足OP要求,我将投票支持您的答案。欢迎使用stackoverflow。您首先回答,并应获得第一次投票:)这是您用SQL而不是C编写的。使用其真正强大的工具。
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
WHERE ((ListPrice >= 400) and (ListPrice <= 800) AND (Color = 'Red')) OR (Color = 'Black')