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
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) '? –
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