如果不等于0,则从列中选择mysql,否则从另一列中选择

时间:2014-02-17 05:46:13

标签: mysql where-clause

我有这个查询

            SELECT 
                products.*, colors.name AS colorName, makes.name AS makeName, genders.name AS genderName,
                GROUP_CONCAT(DISTINCT categories.name ORDER BY categories.name) AS distCategories, 
                GROUP_CONCAT(DISTINCT subcategories.name ORDER BY subcategories.name) AS distSubcategories, 
                GROUP_CONCAT(DISTINCT makes.name ORDER BY makes.name) AS distMakes, 
                GROUP_CONCAT(DISTINCT products_availability.size ORDER BY products_availability.size) AS distSizes 
            FROM products 
                LEFT JOIN colors ON products.color = colors.id 
                LEFT JOIN makes ON products.make = makes.id 
                LEFT JOIN genders ON products.gender = genders.id 
                LEFT JOIN products_and_categories ON products_and_categories.product = products.id 
                LEFT JOIN products_and_subcategories ON products_and_subcategories.product = products.id 
                LEFT JOIN products_availability ON products_availability.product = products.id 
                LEFT JOIN categories ON products_and_categories.category = categories.id 
                LEFT JOIN subcategories ON products_and_subcategories.subcategory = subcategories.id 
            WHERE products.gender IN(5, 6, 7) AND products.type = 5 AND $where 
            GROUP BY(products.id)
            ORDER BY products.id ASC

我希望能够根据价格选择一些产品。

如果列sale_price不是0,那么我想说WHERE sale_price <= :value,但如果sale_price为0,我想说WHERE price <= :value

基本上在按价格过滤结果时,如果产品在售并且有sale_price,我不想考虑原价,否则,如果它不在售我将在price列上执行查询。

我尝试了CASE

$where = 'CASE WHEN products.sale_price > 0 THEN products.sale_price <= : value WHEN products.sale_price = 0 THEN products.price <= : value ';

但这不起作用。

另请注意,该列的默认值为0,而不是DIGIT

有关如何构建查询的任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:1)

在您的CASE声明中进行少量修改后,我想您可以使其正常工作。尝试类似:

SELECT 
products.*, 
colors.name AS colorName, 
makes.name AS makeName,  
genders.name AS genderName,
GROUP_CONCAT(DISTINCT categories.name ORDER BY categories.name) AS distCategories, 
GROUP_CONCAT(DISTINCT subcategories.name ORDER BY subcategories.name) AS distSubcategories, 
GROUP_CONCAT(DISTINCT makes.name ORDER BY makes.name) AS distMakes, 
GROUP_CONCAT(DISTINCT products_availability.size ORDER BY products_availability.size) AS distSizes 
FROM products 
LEFT JOIN colors ON products.color = colors.id 
LEFT JOIN makes ON products.make = makes.id 
LEFT JOIN genders ON products.gender = genders.id 
LEFT JOIN products_and_categories ON products_and_categories.product = products.id 
LEFT JOIN products_and_subcategories ON products_and_subcategories.product = products.id 
LEFT JOIN products_availability ON products_availability.product = products.id 
LEFT JOIN categories ON products_and_categories.category = categories.id 
LEFT JOIN subcategories ON products_and_subcategories.subcategory = subcategories.id 
WHERE products.sale_price = CASE WHEN products.sale_price > 0 
THEN products.sale_price <=  value ELSE products.price <=  value END
AND products.gender IN(5, 6, 7) 
AND products.type = 5 
GROUP BY(products.id)
ORDER BY products.id ASC;