拥有元素相等表,计算“唯一”元素的数量

时间:2018-12-15 20:37:38

标签: mysql combinations

我有两个表: 产品ID表:

表p_test:

id:

1

2

3

4

以及一张产品与所有其他产品进行相等性测试的所有可能组合的表格(不要说我测试了他们的图片并且图片是否相等-我认为它们是同一产品): 表格p_match_test:

p_id_1, p_id_2, is_same

1       2       0

1       3       0

1       4       1

2       3       1

2       4       0

3       4       0

如您在本示例中看到的,共有4种产品。测试产品1与产品4的相等性,发现它们“相同”,与产品2和3相同。 这意味着只有两个“独特”产品(4个产品有2张独特图片)。

我想要实现的是在任何一组产品中都有唯一的图片,这样结果将是:

products_count unique_products_count
4              2

我尝试了以下查询:

select count(p.id)                                                                                                            as products_count,
       sum(if(not exists(select * from p_match_test t where (p.id = t.p_id_1 or p.id = t.p_id_2) and t.is_same = 1), 1, 0)) as unique_products_count
from p_test p

,但它仅计算具有重复项的产品,而不是计算“唯一”图像。 我如何实现我的目标?

0 个答案:

没有答案
相关问题