2017-12-21 3 views
3

Im Versuch, eine SQL-Abfrage auf der äquivalenten Verwendung von OUTER zu finden GILT von MSSQL zu PostgreSQL, aber es scheint schwer zu finden.Was ist die äquivalent Syntax für Outer Nehmen in PostgreSQL

Meine MSSQL Beispielabfrage ist wie diese.

hoffe jemand kann mir bei meinem Problem helfen. Danke im Voraus.

SELECT table1.col1, table1.col2, Supp.ID, Supp.Supplier 

FROM SIS_PRS table1 

OUTER APPLY (SELECT TOP 1 ID, SupplierName FROM table2 WHERE table2.ID = table1.SupplierID) AS Supp 

Antwort

2

Es ist eine seitliche verbinden:

SELECT table1.col1, table1.col2, Supp.ID, Supp.Supplier 
FROM SIS_PRS table1 LEFT JOIN LATERAL 
    (SELECT ID, SupplierName 
     FROM table2 
     WHERE table2.ID = table1.SupplierID 
     FETCH FIRST 1 ROW ONLY 
    ) Supp; 

Sie jedoch ziemlich nah dran mit nur einer korrelierten Unterabfrage in einer Datenbank kommen kann:

SELECT table1.col1, table1.col2, table1.SupplierID, 
     (SELECT ID, Name 
     FROM table2 
     WHERE table2.ID = table1.SupplierID 
     FETCH FIRST 1 ROW ONLY 
     ) as SupplierName 
FROM SIS_PRS table1; 

Beachten Sie auch, dass in beiden Datenbanken, Eine Zeile mit ORDER BY abrufen ist verdächtig.

+0

Vielen Dank, ich habe vergessen, die Reihenfolge von auf meiner Probe :) hinzufügen – ayanix

Verwandte Themen