Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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_Sql Server - Fatal编程技术网

Sql 关于连接条件的案例

Sql 关于连接条件的案例,sql,sql-server,Sql,Sql Server,我有两个表数据如下 table1: Order orderitem value O1 B-IV122 10 O2 B-IV144 10 table2: order Productionorder productionitem ProductionValue O1 P1 B-IV122 5 O2 P2

我有两个表数据如下

table1:
Order    orderitem    value    
 O1       B-IV122       10
 O2       B-IV144       10     

table2:
order    Productionorder    productionitem    ProductionValue
 O1         P1               B-IV122               5
 O2         P2               B-IV111               6
 O2         P2               CCC144                6
 O2         P2               CCC000                4
所需输出:

  Order     Productionorder  orderitem   productionitem  value   ProductionValue
    O1         P1             B-IV122     B-IV122         10       5
    O2         P2             B-IV144     B-IV111         10       6
我尝试了以下代码

select order,orderitem,Productionorder,productionitem,value ,ProductionValue  from 
(select order,orderitem,value from table1) t1
left outer join
(select Order,Productionorder,productionitem,ProductionValue from table2)t2
on t1.order = t2.order and t1.orderitem = t2.productionitem
我的查询输出:

 Order     Productionorder  orderitem   productionitem  value   ProductionValue
    O1         P1             B-IV122     B-IV122         10       5
    O2         P2             B-IV144     NULL            10       NULL
我希望采用以“B”和相应值开头的生产项(B-IV111),而不是null。。 (注意:我也需要加入订单和项目)。案例未按预期工作。您能就此向我提出建议吗


提前感谢。

听起来您需要一个
内部连接

select t1.order, t1.orderitem, t1.Productionorder, t2.productionitem, t1.value, t2.ProductionValue
from table1 t1 join
     table2 t2
     on t1.order = t2.order and t1.orderitem = t2.productionitem;
子查询对查询没有任何价值,所以我删除了它们

编辑:

您可以使用
outer apply
作为示例:

select t1.order, t1.orderitem, t1.Productionorder,
       t2.productionitem, t1.value, t2.ProductionValue
from table1 t1 outer apply
     (select top (1) t2.*
      from table2 t2
      where t1.order = t2.order 
      order by (case when t1.orderitem = t2.productionitem then 1 else 2 end)
     ) t2;

注意:这适用于问题中的特定示例。如果您的实际问题更复杂,因为有多行匹配和不匹配,我会要求您提出一个新问题,并提供适当的样本数据、解释和db/sql小提琴将非常有用。

您能否解释这些值:
B-IV144
B-IV111
是如何关联的?在表2中,用户可以维护相同或不同的项目。如果不同,我需要(B-IV111)项目。这背后有什么逻辑吗?或者你在任何情况下都想要特别的B-IV111?我发布了一个这样的答案,但当我注意到问题的最后一段要求替换
NULL
values@Gordon林诺夫..谢谢你的回复。如果我进行内部联接,结果中将不会出现第二条记录。@PeterSmith。非常感谢。我更新了答案。感谢大家的回复和提供建议。非常感谢你们的解决方案。。它正在工作…但是你能帮我处理左(项目,3)吗…为了理解单字段(字段,3),简单地说,告诉服务器从字段中取出前3个字符(在我们的案例中是orderitem)
SELECT t1.[order]
    ,orderitem
    ,value
    ,productionitem
    ,productionvalue
FROM table1 t1
INNER JOIN table2 t2 ON t1.[order] = t2.[order]
    AND LEFT(t1.orderitem, 3) = LEFT(t2.productionitem, 3)