0

这是我的数据

newCatCols
=====
id      sku     name
---------------
1           4001    Black
2           4031    Red
3           4021    Black
4           4041    Blush

products
=====
id      sku     Price
---------------
1           4001    123
2           4031    232
3           4021    453
4           4041    233

这是我的查询

SELECT COUNT( * ) AS numTotal
FROM `products`
INNER JOIN `colors` ON `products`.`sku` = `colors`.`sku`
WHERE (
`name` = 'Apple_Black'
OR `name` = 'Black'
OR `name` = 'Black_Aubergine'
OR `name` = 'Black_Blush'
OR `name` = 'Black_Slate'
OR `name` = 'Black_Turquoise'
OR `name` = 'Red_Black'
OR `name` = 'Sapphire_Black'
OR `name` = 'Multi Apple'
)
AND `status` = '1'
AND `category` = 'sig'
GROUP BY `products`.`sku`
ORDER BY `products`.`rank` ASC

理想情况下,如果我只是改变这条线会是完美SELECT COUNT( * ) AS numTotal的,只是修改它以使其工作?数据当前返回行值为 1 的产品列表,因此它只是循环而不计算所有行的总值。

它应该只显示返回的总数,例如应该显示 4

Output
 =====
 numTotal
 ---------------
 4
4

1 回答 1

1

这将返回符合您的条件的不同产品 SKU 的数量:

SELECT COUNT(distinct `products`.`sku`) AS numTotal
FROM `products`
INNER JOIN `colors` ON `products`.`sku` = `colors`.`sku`
WHERE `name` in ('Apple_Black'
        ,'Black'
        ,'Black_Aubergine'
        ,'Black_Blush'
        ,'Black_Slate'
        ,'Black_Turquoise'
        ,'Red_Black'
        ,'Sapphire_Black'
        ,'Multi Apple'
    )
    AND `status` = '1'
    AND `category` = 'sig'
于 2012-10-30T15:33:51.507 回答