SQL查询生成没有结果

时间:2010-01-15 15:20:15

标签: sql mysql

我正在尝试使此查询有效:

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表中没有StockFeatures.Features行,它就不会返回任何结果 - 这通常不会出现。如果该表中没有任何内容,我该怎么做才能得到NULL值

该表包含Sku和Features列(Sku应与Stock.Sku列链接)。

任何帮助都将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分: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