我想知道如果不重复4次,显示所有id_product的正确方法是什么。 这是我的表:
id_product id_related
55 1
55 2
55 3
55 4
11 1
11 123
11 12
36 12
36 9
36 14
36 654
我需要找到没有添加4个相关产品的产品。 在这种情况下,我期望的结果是11。
答案 0 :(得分:3)
这样的事情
select id_product, count(*)
from <table>
group by id_product
having count(*) < 4
答案 1 :(得分:2)
以下查询
SELECT id_product
FROM table
GROUP BY id_product
HAVING COUNT(id_product) < 4
答案 2 :(得分:0)
Select
p.id_product,
p.id_related
from product p
join
(
select
id_product,
count(id_related) as tot
from product
group by id_product
having tot <> 4
)p1
on p1.id_product = p2.id_product
如果你想过滤掉重复的id_related意味着多次出现相同的id_related被计算为一个你可以使用distinct as
count(distinct id_related)