Domanda

Two tables - Suppliers and Work_Orders.

I am attempting to return the Supplier where the Supplier_ID is not 0 in the Work_Orders Table. but try as I will it will only return records where there is a record in the Suppliers table... Any ideas? Thanks

SELECT
    Work_Orders.Order_ID as 'ID',
    Work_Orders.Log_Date as 'Date', 
    IF Work_Orders.Supplier_ID > 0 THEN
        Suppliers.Supplier_Name
    ELSE
       'Not Allocated'
    END IF AS 'Supplier', 
    Work_Orders.Add1 as 'Unit',
    Work_Orders.Work_Type as 'Type',
    Work_Orders.Status as Status_ID
FROM
    Work_Orders 
    JOIN Suppliers ON Suppliers.Supplier_ID = Work_Orders.Supplier_ID
WHERE
    Work_Orders.Supplier_ID > 0

In case anyone else runs into this

SELECT Work_Orders.Order_ID as 'ID', Work_Orders.Log_Date as 'Date', IsNull(Suppliers.Supplier_Name, 'Not Allocated') as 'Supplier', Work_Orders.Add1 as 'Unit', Work_Orders.Work_Type as 'Type', Work_Orders.Status as Status_ID FROM Work_Orders 
        LEFT OUTER JOIN Suppliers ON Suppliers.Supplier_ID = Work_Orders.Supplier_ID
È stato utile?

Soluzione

Change your query to use a LEFT OUTER JOIN.

With an INNER JOIN rows are only included in the output if there's a match in both referenced tables, with an OUTER JOIN all rows are included, missing referenced values are substituted with NULL.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top