在mysql查询中缺少数据,需要在内连接中使用条件语句

时间:2016-08-22 16:45:09

标签: mysql

运行以下查询时,我遇到了丢失数据的问题。 有些产品有特殊价格,存储在表oc_product_special中,有些产品的常规价格存储在表oc_product中。 我发现如果表oc_product_special中有特殊价格,它只显示数据。它省略了没有特价和正常价格的任何数据。我不知道如何解决这个问题。如何以及在哪里可以添加条件语句或类似

  

如果有正常价格,没有特价则显示正常价格,0表示特价。

SELECT       
        pd.name AS 'Product Name', 
        p.model AS UPC,  
        p.quantity AS 'Quantity', 
        p.price AS 'Regular Price', 
        ps.price AS 'Special Price', 
        p.cost AS 'COST', 
        p.status AS 'Status'
FROM oc_product p
    INNER JOIN oc_product_description pd
        ON pd.product_id = p.product_id
    INNER JOIN oc_product_special ps
        ON ps.product_id = p.product_id
    INNER JOIN oc_manufacturer m
        ON p.manufacturer_id = m.manufacturer_id
    INNER JOIN oc_product_to_category p2c 
        ON p2c.product_id = p.product_id
    INNER JOIN oc_category c 
        ON c.category_id = p2c.category_id
    INNER JOIN oc_category_description cd 
        ON c.category_id = cd.category_id
WHERE 
      c.category_id = 37 OR c.parent_id = 37
GROUP BY pd.name ORDER BY m.name ASC 

2 个答案:

答案 0 :(得分:1)

使用LEFT JOIN,即使它们与右侧的任何内容不匹配,也会在联接的左侧保留记录:

SELECT COALESCE(pd.name, 'NA') AS 'Product Name', 
       p.model AS UPC,  
       p.quantity AS 'Quantity', 
       p.price AS 'Regular Price', 
       COALESCE(ps.price, 0.0) AS 'Special Price', 
       p.cost AS 'COST', 
       p.status AS 'Status'
FROM oc_product p
LEFT JOIN oc_product_description pd
    ON pd.product_id = p.product_id
LEFT JOIN oc_product_special ps
    ON ps.product_id = p.product_id
INNER JOIN oc_manufacturer m
    ON p.manufacturer_id = m.manufacturer_id
INNER JOIN oc_product_to_category p2c 
    ON p2c.product_id = p.product_id
INNER JOIN oc_category c 
    ON c.category_id = p2c.category_id
INNER JOIN oc_category_description cd 
    ON c.category_id = cd.category_id
WHERE c.category_id = 37 OR
      c.parent_id = 37
GROUP BY pd.name
ORDER BY m.name

<强>解释

LEFT JOIN中,当联接左侧的记录与右侧的任何内容匹配时,右侧的列将全部显示为{{ 1}}在结果集中。我在查询中使用了NULL函数,如果前者为COALESCE,它将有条件地将第一个参数替换为第二个参数。在这种情况下,如果NULL,特殊价格将被替换为零。我也将它与产品名称一起使用,以防在某些情况下缺少名称。

答案 1 :(得分:0)

在oc_product_special上使用左连接

    SELECT       
            pd.name AS 'Product Name', 
            p.model AS UPC,  
            p.quantity AS 'Quantity', 
            p.price AS 'Regular Price', 
            ps.price AS 'Special Price', 
            p.cost AS 'COST', 
            p.status AS 'Status'
    FROM oc_product p
        INNER JOIN oc_product_description pd
            ON pd.product_id = p.product_id
        LEFT  JOIN oc_product_special ps
            ON ps.product_id = p.product_id
        INNER JOIN oc_manufacturer m
            ON p.manufacturer_id = m.manufacturer_id
        INNER JOIN oc_product_to_category p2c 
            ON p2c.product_id = p.product_id
        INNER JOIN oc_category c 
            ON c.category_id = p2c.category_id
        INNER JOIN oc_category_description cd 
            ON c.category_id = cd.category_id
    WHERE 
          c.category_id = 37 OR c.parent_id = 37
    GROUP BY pd.name ORDER BY m.name ASC 

在你的情况下匹配值的内连接si不匹配所以..use left join