如何在sql中连接表的两列和其他表的两列

时间:2017-01-04 22:24:20

标签: sql sql-server

我在#temp表上有2列 SKU_new order_ID ,与 SKU 匹配订单表的ID 列我正在使用内部联接,但它一直在执行,并且不显示任何结果。

我的查询是

string sString = "S104";
string sString2 = "AS105";
string sString3 = "ASRVT106";

var re = new Regex(@"\d+");

Console.WriteLine(re.Match(sString).Value); // 104
Console.WriteLine(re.Match(sString2).Value); // 105
Console.WriteLine(re.Match(sString3).Value); // 106

3 个答案:

答案 0 :(得分:0)

在阅读您的问题时,您说“2列SKU_new order_ID”

基于此,你不会在你的Join中使用AND代替OR吗?

SELECT  t.SKU_new
,       t.ID_Order
INTO    #caseone
FROM    dbo.Order_No AS n
        JOIN dbo.Orders AS o ON n.Order_No = o.Order_No
        JOIN #Temp AS t ON t.ID_Order = o.ID
                           AND t.SKU_new = o.SKU;

答案 1 :(得分:0)

有两种情况,Query继续执行

一个是表上有未提交的事务 然后试试

 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
        SELECT t.SKU_new, t.ID_Order
        INTO #caseone
        FROM dbo.Order_No AS n JOIN
       dbo.Orders AS o
       ON n.Order_No = o.Order_No JOIN
       #Temp AS t
       ON (t.ID_Order = o.ID or t.SKU_new = o.SKU)

如果是方案2,它可能会有很多记录错误的索引。要检查是否正在获取记录,请在选择

中尝试使用Top
        SELECT Top 1  t.SKU_new, t.ID_Order
        INTO #caseone
        FROM dbo.Order_No AS n JOIN
       dbo.Orders AS o
       ON n.Order_No = o.Order_No JOIN
       #Temp AS t
       ON (t.ID_Order = o.ID or t.SKU_new = o.SKU

答案 2 :(得分:0)

我认为这可能就是你要找的东西。

SELECT t.SKU_new
, t.ID_Order
INTO #caseone
FROM #temp t 
JOIN dbo.orders o 
    ON t.ID_Order = o.ID 
    AND t.SKU_new = o.SKU
JOIN dbo.Order_No n 
    ON n.Order_No = o.Order_No