Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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
带嵌套where子句的sql连接_Sql_Sql Server_Join - Fatal编程技术网

带嵌套where子句的sql连接

带嵌套where子句的sql连接,sql,sql-server,join,Sql,Sql Server,Join,我在尝试连接两个表时遇到了一个问题,示例表如下 订单表(Odr) 项目位置表(Loc) 我想看到的是,对于订单类型1、2和3的每个项目,以及对于需求数量大于0的订单,位置X的数量 如下图所示 Order_No Item No Order_Type Req_Qty X_Qty 100 A 2 45 100 101 B 1 32 150 102 F 2

我在尝试连接两个表时遇到了一个问题,示例表如下

订单表(Odr)

项目位置表(Loc)

我想看到的是,对于订单类型1、2和3的每个项目,以及对于需求数量大于0的订单,位置X的数量 如下图所示

Order_No Item No  Order_Type  Req_Qty X_Qty
100        A          2        45     100
101        B          1        32     150
102        F          2        23     250
104        C          3        14     75
现在我写了一个如下的查询,但我觉得它没有给我正确的结果

select Odr.*, Loc.Qty
from Odr 
inner JOIN Loc
ON Odr.ITEM_no = Loc.ITEM_no
where (SOPTYPE = '1' and Req_Qty >0
 or SOPTYPE = '2' and Req_Qty >0
 or SOPTYPE = '3' and Req_Qty >0) AND Loc.Location = 'X' 
有人能帮我检查一下,这是否是我想要的结果的正确方法吗


谢谢

查询看起来很好。但你可以让它更具可读性:

select Odr.*, Loc.Qty
from Odr 
inner JOIN Loc
ON Odr.ITEM_no = Loc.ITEM_no
where (SOPTYPE = '1' or SOPTYPE = '2' or SOPTYPE = '3') 
  and Req_Qty > 0 
  and Loc.Location = 'X' 


添加括号…@jarlh-我觉得在这种情况下不会有任何区别我觉得查询很好我猜是关于
左连接
或者甚至是
外部应用
和订单类型上的附加过滤器以及其他东西。但我觉得它没有给我正确的结果意味着什么?
select Odr.*, Loc.Qty
from Odr 
inner JOIN Loc
ON Odr.ITEM_no = Loc.ITEM_no
where (SOPTYPE = '1' and Req_Qty >0
 or SOPTYPE = '2' and Req_Qty >0
 or SOPTYPE = '3' and Req_Qty >0) AND Loc.Location = 'X' 
select Odr.*, Loc.Qty
from Odr 
inner JOIN Loc
ON Odr.ITEM_no = Loc.ITEM_no
where (SOPTYPE = '1' or SOPTYPE = '2' or SOPTYPE = '3') 
  and Req_Qty > 0 
  and Loc.Location = 'X' 
select Odr.*, Loc.Qty
from Odr 
inner JOIN Loc
ON Odr.ITEM_no = Loc.ITEM_no
where SOPTYPE IS IN('1', '2', '3') 
  and Req_Qty > 0 
  and Loc.Location = 'X'