0

我以为我可以解决这个问题,但我遇到了很多问题。我有 3 个表Table1、、、Table2Table3。这些桌子是别人设计的,我必须和他们一起工作。它们的设计初衷不是按照今天的方式使用。

底线是我需要能够输入Item_No,这将始终存在于Table2. 如果也可以在 中找到可能是多次或没有,有时我可以在 中找到 5 次,在 中只能找到 3 次。如果它在它也将在.Item_NoTable 3Table2Table3Table3Table1

因此,使用我可以在 Table2 上找到的 Item_No,返回与这些行关联的 Order_qty。然后使用 if exists 获取 Table1.IDwhere Table1.ID = Table3.IDWHERETable3.Item_No = Table2.Item_No

我想出了以下内容,它不会给我错误,而只是在 C# 填充期间停止代码执行。我让它用于查找并返回它找到的Item_No内容Table3,我只更改了这行代码,所以我知道这是问题所在。

这是我能想出的不起作用的方法:

SELECT Table1.ID, 
       Table2.Order_Qty As [Qty of Full Order], Table2.Item_No As [Set No] 
FROM Table2 
LEFT JOIN Table3 
       ON  Table2.Item_No = Table3.Item_No 
           AND Table2.Order_No = Table3.Order_No
LEFT JOIN Table1 
       ON Table1.Order_No = Table2.Order_No 
          AND Table1.ID = Table3.ID 
WHERE Table2.Item_No = @m_strUserEnteredSeachValue
ORDER BY Table2.Order_No DESC

*示例数据:*

表格1

+----------+--------------+-------------------+
| Order_No | Sub_Order_No | Sub_Order_Contact |
+==========+==============+===================+
|    1     |      1       |     John Doe      |
+----------+--------------+-------------------+
|    1     |      2       |     Jane Doe      |
+----------+--------------+-------------------+
|    1     |      3       |        Foo        |
+----------+--------------+-------------------+
|    1     |      4       |        Bar        |
+----------+--------------+-------------------+
|    1     |      5       |        Foo2       |
+----------+--------------+-------------------+

表 2

+----------+--------------+-------------------+
| Order_No |    Item_No   | Customer_Item_Name|
+==========+==============+===================+
|    1     |      1       |   1234567890      |
+----------+--------------+-------------------+
|    1     |      2       |   1234567891      |
+----------+--------------+-------------------+
|    1     |      3       |   1234567892      |
+----------+--------------+-------------------+
|    1     |      4       |   1234567893      |
+----------+--------------+-------------------+
|    1     |      5       |   1234567894      |
+----------+--------------+-------------------+
|    1     |      6       |   1234567895      |
+----------+--------------+-------------------+
|    2     |      1       |    0987654321     |
+----------+--------------+-------------------+
|    2     |      2       |    0987654322     |
+----------+--------------+-------------------+
|    2     |      3       |    0987654323     |
+----------+--------------+-------------------+
|    3     |      1       |    1234567893     |
+----------+--------------+-------------------+

和表 3

+----------+--------------+-------------------+--------------+
| Order_No |    Item_No   | Customer_Item_Name| Sub_Order_No |
+==========+==============+===================+==============+
|    1     |      1       |   1234567890      |       1      |
+----------+--------------+-------------------+--------------+
|    1     |      2       |   1234567891      |       2      |
+----------+--------------+-------------------+--------------+
|    1     |      3       |   1234567892      |       2      |
+----------+--------------+-------------------+--------------+
|    1     |      4       |   1234567893      |       3      |
+----------+--------------+-------------------+--------------+
|    1     |      5       |   1234567894      |       4      |
+----------+--------------+-------------------+--------------+
|    1     |      6       |   1234567895      |       4      |
+----------+--------------+-------------------+--------------+
|    1     |      4       |   1234567893      |       4      |
+----------+--------------+-------------------+--------------+

我正在寻找的结果:如果我搜索项目 1234567893

+----------+--------------+-------------------+--------------+-------------------+
| Order_No |    Item_No   | Customer_Item_Name| Sub_Order_No | Sub_Order_Contact |
+==========+==============+===================+==============+===================+
|    3     |      1       |   1234567893      |              |                   |
+----------+--------------+-------------------+--------------+-------------------+
|    1     |      4       |   1234567893      |      3       |       Foo         |
+----------+--------------+-------------------+--------------+-------------------+
|    1     |      4       |   1234567893      |      4       |       Bar         |
+----------+--------------+-------------------+--------------+-------------------+
4

2 回答 2

0

对此类问题的务实答案是将其拆分为几个查询。首先查询表#2,然后根据该结果集,对#1 或#3 运行其他查询。

另一个角度是查询表#2 并使用子查询到达表#1 或表#3 以获取您需要的数据。

于 2013-04-29T20:00:22.790 回答
0

尝试这个:

declare @m_strUserEnteredSeachValue varchar(10) = '1234567893';
with a as
(
  select 
    Order_No, Item_No, Customer_Item_Name 
  from 
    Table2
  UNION 
  select 
    Order_No, Item_No, Customer_Item_Name 
  from 
    Table3
)
select 
  a.Order_No, 
  a.Item_No, 
  a.Customer_Item_Name,
  Table3.Sub_Order_No, 
  Table1.Sub_Order_Contact 
from 
  a
left join 
  Table3 
on 
  Table3.Order_No=a.Order_No 
  and Table3.Item_No=a.Item_No 
  and Table3.Customer_Item_Name=a.Customer_Item_Name
left join 
  Table1 
on
  Table1.Sub_Order_No = Table3.Sub_Order_No
where 
  @m_strUserEnteredSeachValue = a.Customer_Item_Name
order by 
  a.Item_No, Table3.Sub_Order_No

SqlFiddle 演示:http ://www.sqlfiddle.com/#!3/973d8/3

我不知道这是否是您要达到的目标,因为很难从您的问题中理解。我只知道这个查询给出了你放在 OP 中的数据集。

于 2013-04-29T23:49:48.437 回答