【问题标题】:I need to return results from a 3 table join when one table has no records当一个表没有记录时,我需要从 3 个表连接返回结果
【发布时间】:2016-05-27 20:37:11
【问题描述】:

我已经为此工作了几天,还没有找到修复或解决方案。即使一个表(table3)没有匹配的数据,我也试图返回所有结果。我需要返回的默认值是 table1.ord_no、table2.item_no、table1.cus_no、table2.unit_price、table2.item_desc_1。如果 table3 中没有匹配项可以为我提供 .prc_or_disc_1 字段的匹配项,我只需要回显 0。但是由于我的查询现在就坐,只要 table3 为空,我假设由于我的 AND table2,它不会返回任何内容。 cus_no = table3.cd_tp_1_cust_no 语句。但是从 table1 和 table2 获得结果会很棒。我尝试使用左连接、左外连接和外连接。有什么解决办法吗?

 SELECT table1.ord_no, table2.item_no, table2.item_desc_1,
      table1.cus_no, table2.unit_price, table3.prc_or_disc_1, table2.line_seq_no
    FROM table2 JOIN table1
        ON table1.ord_no = table2.ord_no
      LEFT OUTER JOIN table3 on table2.item_no = table3.cd_tp_1_item_no
    Where table2.ord_no = $multi_orders
      AND table2.cus_no = table3.cd_tp_1_cust_no
     AND getdate() BETWEEN start_dt AND end_dt
    ORDER BY table2.line_seq_no

我也尝试了这个荒谬的查询,它几乎成功了,但给了我一个错误,说我不能收到多个结果。

IF (SELECT table3.prc_or_disc_1
FROM table3 join table1 on table1.cus_no = table3.cd_tp_1_cust_no
JOIN table2
ON table1.ord_no = table2.ord_no WHERE table1.ord_no = $multi_orders
AND table3.cd_tp_1_item_no = table2.item_no
AND table3.cd_tp_1_cust_no = table1.cus_no
AND getdate() between start_dt and end_dt) > 0
BEGIN
(SELECT table1.ord_no
, table2.item_no
, table2.item_desc_1
,table1.cus_no
, table2.unit_price
, table3.prc_or_disc_1
FROM table1
JOIN table2
ON table1.ord_no = table2.ord_no
JOIN cicmpy ON table1.cus_no = cicmpy.debcode
LEFT JOIN table3 on table1.cus_no = table3.cd_tp_1_cust_no
WHERE table1.ord_no = $multi_orders
AND table3.cd_tp_1_item_no = table2.item_no
AND table3.cd_tp_1_cust_no = table1.cus_no
AND getdate() between start_dt and end_dt)
END
ELSE
BEGIN
SELECT table1.ord_no
, table2.item_no
, table2.item_desc_1
,table1.cus_no
, table2.unit_price
FROM table1
JOIN table2
ON table1.ord_no = table2.ord_no
JOIN cicmpy ON table1.cus_no = cicmpy.debcode
WHERE table1.ord_no = $multi_orders;
END

【问题讨论】:

  • 不用过多讨论,也许您可​​以在您的条件AND (table2.cus_no = table3.cd_tp_1_cust_no OR table3.cd_tp_1_cust_no IS NULL) 中允许 table3 值为空?
  • 不行,我也试过了,table2.cus_no = table3.cd_tp_1_cust_no OR table3.cd_tp_1_cust_no IS NULL AND table2.cus_no IS NULL。

标签: php sql-server-2008


【解决方案1】:

您应该将条件 table2.cus_no = table3.cd_tp_1_cust_no 放入 LEFT OUTER JOIN 条件而不是 WHERE 条件中。 当table3 中没有匹配的行时,这不仅会给你一个结果,而且在我看来,执行这个更窄的连接而不是稍后过滤更合乎逻辑。

因此您的查询将变为:

SELECT table1.ord_no, table2.item_no, table2.item_desc_1,
        table1.cus_no, table2.unit_price, table3.prc_or_disc_1, table2.line_seq_no
    FROM table2 JOIN table1 ON table1.ord_no = table2.ord_no
        LEFT OUTER JOIN table3 ON table2.item_no = table3.cd_tp_1_item_no
            AND table2.cus_no = table3.cd_tp_1_cust_no AND getdate() BETWEEN start_dt AND end_dt
    WHERE table2.ord_no = $multi_orders
    ORDER BY table2.line_seq_no

【讨论】:

  • 我也试过了。它也不起作用 LEFT OUTER JOIN table3(在 table2.item_no = table3.cd_tp_1_item_no AND table2.cus_no = table3.cd_tp_1_cust_no 上)正确吗?
  • @Ryan 除了你的括号必须在on 之后,这应该可以工作。我唯一能想到的另一件事是,table3.prc_or_disc1NULL。你需要COALESCE(table3.prc_or_disc1, 0) 吗?
  • 我还不熟悉 Coalesce,它有什么作用?
  • @Ryan 它返回第一个参数,不是NULL。所以在这个简单的应用程序中,如果存在table3.prc_or_disc1,它将返回它,否则返回0
  • 我试着把它放在 select 语句中,没有运气。
猜你喜欢
  • 2012-01-03
  • 1970-01-01
  • 2015-04-11
  • 2020-04-05
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
相关资源
最近更新 更多