私は数日間この作業をしていますが、未解決の問題や解決策はまだありません。 1つのテーブル(table3)に一致するデータがない場合でも、すべての結果を返そうとしています。私が返す必要があるデフォルトは、table1.ord_no、table2.item_no、table1.cus_no、table2.unit_price、table2.item_desc_1です。 .prc_or_disc_1フィールドに一致するものがtable3にない場合、私は0にエコーする必要があります。しかし、私のANDクエリtable3が空であると仮定しているときは、今すぐクエリが返されます。 cus_no = table3.cd_tp_1_cust_no文。しかし、table1とtable2の結果を得ることは素晴らしいことです。私は、左の結合、左の外部結合、および外部結合を使用しようとしました。修正はありますか?1つのテーブルにレコードがないときに3テーブル結合の結果を返す必要があります
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
あまりそれに入るがなければ、多分あなただけの表3の値は、あなたの基準でnullにすることを可能に 'AND(table2.cus_no = table3.cd_tp_1_cust_noができOR table3.cd_tp_1_cust_no IS NULL) '? –
それはうまくいきません、私も試しました、table2.cus_no = table3.cd_tp_1_cust_no OR table3.cd_tp_1_cust_noはNULLで、table2.cus_noはNULLです。 – Ryan