2010-01-15 150 views
0

我試圖讓這個查詢工作:SQL查詢不產生任何結果

SELECT Stock.*, 
     StockFeatures.Features, 
     StockDescriptions.Detailed, 
     StockDescriptions.Technical, 
     PRD1.RuleValue as Price, 
     PRD2.RuleValue as WasPrice, 
     PRD2.RuleValue-PRD1.RuleValue as Save, 
     PRD1.Quantity 
FROM 
    StockFeatures, Stock INNER JOIN 
    PriceRuleDetail PRD1 ON PRD1.Sku = Stock.Sku 
     AND PRD1.PriceRule = 'RG' LEFT JOIN 
    PriceRuleDetail PRD2 ON PRD2.Sku = Stock.Sku 
     AND PRD2.PriceRule = 'RRP' LEFT JOIN 
    StockDescriptions ON StockDescriptions.Sku = Stock.Sku 
WHERE Stock.GeneralStkStatus < 3 
AND Stock.Sku = '11044' 
AND StockFeatures.Sku = Stock.Sku 
ORDER BY PRD1.Quantity ASC 

它時,沒有一個StockFeatures.Features行的StockFeatures表不返回任何結果 - 這也經常不會是。我該如何使它只在該表中沒有任何內容時纔會出現NULL值?

該表具有Sku和特徵(Sku應與Stock.Sku列鏈接)列。

任何幫助,將不勝感激。

在此先感謝。

+1

我很抱歉,但如果沒有StockFeature(如你所說,它可能爲空),你想加入該怎麼行Stock.Sku ?????或者你只是想要一整行的空值? – gbianchi 2010-01-15 15:25:49

回答

7

您需要LEFT JOINStockFeatures

例如:(未經測試)

SELECT Stock.*, 
     StockFeatures.Features, 
     StockDescriptions.Detailed, 
     StockDescriptions.Technical, 
     PRD1.RuleValue as Price, 
     PRD2.RuleValue as WasPrice, 
     PRD2.RuleValue-PRD1.RuleValue as Save, 
     PRD1.Quantity 
FROM 
    Stock LEFT JOIN 
    StockFeatures ON Stock.Sku = StockFeatures.Sku INNER JOIN 
    PriceRuleDetail PRD1 ON PRD1.Sku = Stock.Sku 
     AND PRD1.PriceRule = 'RG' LEFT JOIN 
    PriceRuleDetail PRD2 ON PRD2.Sku = Stock.Sku 
     AND PRD2.PriceRule = 'RRP' LEFT JOIN 
    StockDescriptions ON StockDescriptions.Sku = Stock.Sku 
WHERE Stock.GeneralStkStatus < 3 
AND Stock.Sku = '11044' 
ORDER BY PRD1.Quantity ASC 
+0

你能舉個例子嗎?我試過這樣做,但它似乎每次都會出錯!對SQL來說很新穎:(。乾杯。 – Schodemeiss 2010-01-15 15:23:14

+2

你會得到什麼錯誤? – 2010-01-15 15:25:53

+1

這個答案絕對是完美的。非常感謝你! – Schodemeiss 2010-01-15 15:29:40

相關問題