Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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 括号中使用的OR运算符以及where子句返回不同的结果。_Sql_Sql Server_Where Clause - Fatal编程技术网

Sql 括号中使用的OR运算符以及where子句返回不同的结果。

Sql 括号中使用的OR运算符以及where子句返回不同的结果。,sql,sql-server,where-clause,Sql,Sql Server,Where Clause,以下两条SQL语句之间的区别是什么: SELECT * FROM Customers WHERE Country='France' OR Country='Mexico' 及 为什么这两个语句返回不同的结果?没有区别 declare @t table (id int identity (1,1), country varchar(20), b int) insert @t (country, b) values ('france',1),('france',2),('france',3),(

以下两条SQL语句之间的区别是什么:

SELECT * FROM Customers
WHERE Country='France' OR Country='Mexico'


为什么这两个语句返回不同的结果?

没有区别

declare @t table (id int identity (1,1), country varchar(20), b int)

insert @t (country, b) values ('france',1),('france',2),('france',3),('mexico',1),('mexico',4), ('brazil',1)

select * from @t where (country = 'france' or country = 'mexico') 
select * from @t where country = 'france' or country = 'mexico'
-- both return rows 1-5

但是,如果您添加了另一个子句(如<代码>和<代码>),则需要考虑哪些子句优先考虑。

select * from @t 
where (country = 'france' or country = 'mexico') and b = 1

-- returns  rows 1 and 4

select * from @t 
where country = 'france' or country = 'mexico'  and b = 1

-- returns rows 1-4

这个查询没有区别,这两个查询应该返回相同的结果——我已经对此进行了事件测试。这是肯定的。clouse根据逻辑法则工作的SQL:


因此,本例中的括号不起任何作用

这两种说法没有区别。你能和我们分享结果吗?您向我们显示的查询是您正在使用的整个语句,还是在所涉及的
子句中存在不同的
?如果我再添加一个AND子句,那么为什么这两个语句返回不同的结果?因为AND具有优先权。就像数学一样,x优先于数学+
select * from @t 
where (country = 'france' or country = 'mexico') and b = 1

-- returns  rows 1 and 4

select * from @t 
where country = 'france' or country = 'mexico'  and b = 1

-- returns rows 1-4