I had written a sql query long time ago and when I was revisiting that query, I got confused in the where condition of the query. Here is the brief intro on what the sql should do: The sql tries to look for all the orders that were closed yesterday or all the orders that were in open status and has been open for more than 10 days.
我很久以前写过一个sql查询,当我重新访问该查询时,我对查询的where条件感到困惑。以下是sql应该做什么的简要介绍:sql尝试查找昨天关闭的所有订单或者处于打开状态并且已经打开超过10天的所有订单。
Here is the sql code
这是sql代码
DECLARE
@start_date as datetime,
@end_date as datetime
SET @start_date = dateadd(dd, datediff(dd,0,getdate())-2, 0)
SET @end_date = dateadd(dd, datediff(dd,0,getdate())-1, 0)
Select * from OrdersTable
---- all columns are coming from one table only
Where
(
(Closed Date>=@start_date and Closed Date<@end_date and order_status='Closed' )
or
(order_Status='Open')---this tells us the cases is open
and
datediff(ss,OrderStarDate,
dateadd(dd,datediff(dd,0,getdate()),0))/86400.0 >= 10
)
NOW THE CONFUSION IS THIS...
现在这种混乱......
after the 'or' condition in where clause everything should be under one parenthesis like below:
在where子句中的'或'条件之后,所有内容都应在下面的一个括号内:
Select * from OrdersTable
----all columns are coming from one table only
Where
(
(Closed Date>=@start_date and Closed Date<@end_date and order_status='Closed' )
or (order_Status='Open' ---In above query I mistakenly closed the parantheses here
and
datediff(ss,OrderStarDate,
dateadd(dd,datediff(dd,0,getdate()),0))/86400.0 >= 10
)---the Parentheses should have been closed here
)----The final parentheses like the above query no change here
So I tried my best to explain this question. If anyone can answer: Will my first query where I made mistake will still work as the second query (the correct one). The reason why I am asking is when I correct my sql,I get the same records as when I run the wrong query....I am trying to get a logical answer why this is happening.
所以我尽力解释这个问题。如果有人可以回答:我的第一个查询错误仍然可以作为第二个查询(正确的查询)。我问的原因是当我更正我的sql时,我得到的记录与我运行错误查询时的记录相同....我试图得到一个合乎逻辑的答案为什么会发生这种情况。
1 个解决方案
#1
0
For better Understanding of your Question:
为了更好地理解您的问题:
In your where
clause you have parenthesis
在你的where子句中你有括号
1. ( this means you have a group or enclosed condition with a two Contents
in or Operand
2.() and this one is your first condition
or
3.( while this one is your second condition with
4. (Wrapper DateDiff
5. ( second DateDiff inside the wrapper
) End Second DateDiff
)End Wrapper
) End of second condition
) End here
Your Query will do the number 1
first off course but you have an enclosed in number 2
that's why the searching change to number 2
first. After the 2 is done then it will proceed to 3.
您的查询将首先执行第1个关闭课程,但您在第2个数字中包含,这就是搜索首先更改为数字2的原因。 2完成后,它将进入3。
To explain it briefly, It will do first always in a Close
and Open
Parenthesis in every Operand.
简要解释一下,它将首先始终在每个操作数的一个关闭和打开括号中。
#1
0
For better Understanding of your Question:
为了更好地理解您的问题:
In your where
clause you have parenthesis
在你的where子句中你有括号
1. ( this means you have a group or enclosed condition with a two Contents
in or Operand
2.() and this one is your first condition
or
3.( while this one is your second condition with
4. (Wrapper DateDiff
5. ( second DateDiff inside the wrapper
) End Second DateDiff
)End Wrapper
) End of second condition
) End here
Your Query will do the number 1
first off course but you have an enclosed in number 2
that's why the searching change to number 2
first. After the 2 is done then it will proceed to 3.
您的查询将首先执行第1个关闭课程,但您在第2个数字中包含,这就是搜索首先更改为数字2的原因。 2完成后,它将进入3。
To explain it briefly, It will do first always in a Close
and Open
Parenthesis in every Operand.
简要解释一下,它将首先始终在每个操作数的一个关闭和打开括号中。