Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL多重与或_Sql - Fatal编程技术网

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')