Sybase SQL条件连接

时间:2014-04-09 21:21:47

标签: sql sybase

两个表格 - SuppliersWork_Orders

我正在尝试返回Work_Orders表中Supplier_ID不为0的供应商。但尝试我将只返回供应商表中有记录的记录...任何想法?感谢

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

如果有其他人遇到此

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

1 个答案:

答案 0 :(得分:1)

更改您的查询以使用LEFT OUTER JOIN

如果两个引用表中都匹配,则INNER JOIN行仅包含在输出中,并且OUTER JOIN包含所有行,缺少的引用值将替换为NULL