2016-05-27 4 views
0

Ich habe seit Tagen daran gearbeitet und habe noch keine Lösung gefunden. Ich versuche, alle Ergebnisse zurückzugeben, auch wenn eine Tabelle (Tabelle 3) keine übereinstimmenden Daten enthält. Der Standardwert, den ich zurückgeben muss, ist table1.ord_no, table2.item_no, table1.cus_no, table2.unit_price, table2.item_desc_1. Wenn es keine Übereinstimmung in Tabelle3 gibt, die mir eine Übereinstimmung für das .prc_or_disc_1 Feld gibt, muss ich nur 0 echo. Aber wie meine Abfrage jetzt sitzt, wird nichts zurückgeben, wann immer Tabelle3 leer ist, nehme ich wegen meiner UND table2 an. cus_no = table3.cd_tp_1_cust_no-Anweisung. Aber es wäre toll, die Ergebnisse von Tabelle1 und Tabelle2 zu erhalten. Ich habe versucht, eine linke Verbindung, eine linke äußere Verbindung und eine äußere Verbindung zu verwenden. Gibt es irgendeine Lösung?Ich muss Ergebnisse aus einem 3-Tabellen-Join zurückgeben, wenn eine Tabelle keine Datensätze enthält

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 

Ich habe auch versucht, diese lächerliche Abfrage, die fast gearbeitet, aber gab mir einen Fehler, der sagte, ich nicht mehr als ein Ergebnis empfangen kann.

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 
+0

ohne in sie bekommen zu viel, vielleicht könnten Sie einfach erlauben die table3 Wert in Ihren Kriterien null sein 'AND (table2.cus_no = table3.cd_tp_1_cust_no OR table3.cd_tp_1_cust_no IST NULL) '? –

+0

Nein, das funktioniert nicht, ich habe es auch versucht, table2.cus_no = table3.cd_tp_1_cust_no ODER table3.cd_tp_1_cust_no IST NULL UND table2.cus_no IST NULL. – Ryan

Antwort

0

sollten Sie den Zustand table2.cus_no = table3.cd_tp_1_cust_no in den LEFT OUTER JOIN Zustand versetzt statt der WHERE Zustand. Das ergibt nicht nur ein Ergebnis, wenn es in keine übereinstimmende Zeile gibt, sondern es erscheint mir auch logischer, diesen schmaleren Join zu verwenden, anstatt später zu filtern.

So würde Ihre Abfrage werden dann:

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 
+0

Ich habe das auch versucht. Es hat nicht funktioniert entweder LEFT OUTER JOIN table3 (auf table2.item_no = table3.cd_tp_1_item_no UND table2.cus_no = table3.cd_tp_1_cust_no) Richtig? – Ryan

+0

@Ryan Abgesehen davon, dass Ihre Klammer nach "an" kommen müsste, sollte das funktionieren. Die einzige andere Sache, die mir einfällt, ist, dass 'table3.prc_or_disc1'' NULL' ist. Brauchen Sie 'COALESCE (table3.prc_or_disc1, 0)'? – bodo

+0

Ich bin noch nicht vertraut mit Coalesce, was macht das? – Ryan

Verwandte Themen