我有以下查询,其中我想获得总行数,但这似乎在每个单独计算这里是查询和结果
SELECT
COUNT(pc.product_id)
FROM
products as p
INNER JOIN product_categories as pc
ON p.product_id = pc.product_id AND pc.subcategory_id IN (77)
GROUP BY pc.product_id
HAVING COUNT(pc.subcategory_id) = 1
COUNT(pc.product_id)
1
1
1
......但它应该是
COUNT(pc.product_id)
3
更新
上述查询应计算此查询获得的产品数量
SELECT
*
FROM
products as p
INNER JOIN product_categories as pc
ON p.product_id = pc.product_id AND pc.subcategory_id IN ($sb_2)
GROUP BY pc.product_id
HAVING COUNT(DISTINCT pc.subcategory_id) = $sb_count
$sb_2 = 77,76;
$sb_count = how many values are in $sb_2;
77 and 76 are different subcategories ids but are common for some products
如果您知道另一种计算方法或进行其他查询......
答案 0 :(得分:2)
SELECT
COUNT(pc.product_id)
FROM
products as p
INNER JOIN product_categories as pc
ON p.product_id = pc.product_id AND pc.subcategory_id IN (77)
HAVING COUNT(pc.subcategory_id) = 1
删除组应该可以解决问题。
答案 1 :(得分:1)
我设法按以下查询计算行数。
"SELECT COUNT(*) as count FROM (SELECT
*
FROM
product_categories WHERE subcategory_id IN (77,76)
GROUP BY product_id
HAVING COUNT(DISTINCT subcategory_id) = 2) as subc";
}