SELECT用户拥有至少2个相同的产品

时间:2019-08-16 08:01:37

标签: sql postgresql

用户表:

user_id name
1 john
2 mark
3 scott
4 piter

user_products表:

user_id product_id
1 2
1 4
1 5
2 4
2 5
2 7
3 1
3 5
3 4
3 2
4 1

我们看到,用户1,2,3都有产品4和5。那么如何选择至少具有2个相同产品的用户?

5 个答案:

答案 0 :(得分:0)

一种选择是使用自我联接:

SELECT
    u.user_id,
    u.name
FROM user_products up1
INNER JOIN user_products up2
    ON up1.product_id = up2.product_id AND
       up1.user_id <> up2.user_id
INNER JOIN Users u
    ON up1.user_id = u.user_id
GROUP BY
    u.user_id,
    u.name
HAVING
    COUNT(DISTINCT up1.product_id) > 1;

Demo

这里的想法是尝试将给定用户的每个记录与来自不同用户但具有相同产品的记录进行匹配。然后,聚合步骤将检查给定用户在内部联接之后是否仍然至少还有两个产品,这意味着实际上他确实与其他一些用户共有至少两个产品。

此处匹配的用户是:john,mark和scott

答案 1 :(得分:0)

自我加入是正确的方法,但我认为正确的逻辑是:

@Mock
MonitorServiceImpl monitorServiceMock;

@InjectMocks
SomeUserOfMonitorServiceImpl monitorServiceImplUser;

@Test
public void testG() throws Exception {
   String url="http://dsgdfgdf/"
   Mockito.when( monitorServiceMock.replaceContent("prueba/{id}", "{id}", "test"))
            .thenReturn(url));
   //Do Something which calls the monitorService.replaceContent 
   monitorServiceImplUser.doSomething();

如果需要产品列表,则可以包含SELECT up1.user_id, up2.user_id FROM user_products up1 JOIN user_products up2 ON up1.product_id = up2.product_id AND up1.user_id < up2.user_id GROUP BY up1.user_id, up2.user_id HAVING COUNT(DISTINCT up1.product_id) >= 2;

答案 2 :(得分:0)

您也可以尝试以下方法:

select distinct b.user_id from #UserProduct b
join (
select count(1) cnt,product_id from #UserProduct
group by product_id
having count(1) = 3)  c
on b.product_id=c.product_id

答案 3 :(得分:0)

这次希望能对您有所帮助

Select * into #User From (
    Select '1' [user_id], 'john' [user_name] Union All
    Select '2' [user_id], 'mark' [user_name] Union All
    Select '3' [user_id], 'scott' [user_name] Union All
    Select '4' [user_id], 'piter' [user_name] 
) A

Select * into #UserProduct From (
    Select '1' [user_id],  '2' [product_id] union All
    Select '1' [user_id],  '4' [product_id] union All
    Select '1' [user_id],  '5' [product_id] union All
    Select '2' [user_id],  '4' [product_id] union All
    Select '2' [user_id],  '5' [product_id] union All
    Select '2' [user_id],  '7' [product_id] union All
    Select '3' [user_id],  '1' [product_id] union All
    Select '3' [user_id],  '5' [product_id] union All
    Select '3' [user_id],  '4' [product_id] union All
    Select '3' [user_id],  '2' [product_id] union All
    Select '4' [user_id],  '1' [product_id] 
) A

Select U1.[user_id] From (
    Select 
        A.[Product_id] Product_id1, 
        B.[Product_id] Product_id2
    From (
        Select [Product_id] From #UserProduct
        Group By [Product_id]
    ) A
    Left Join (
        Select [Product_id] From #UserProduct
        Group By [Product_id]
    ) B On 1 = 1
    Where A.[Product_id] < B.[Product_id]
) Product2
Left Join (
    Select [user_id] From #UserProduct 
    Group By [user_id]
) [User] On 1 = 1
Left Join #UserProduct U1 On U1.[user_id] = [User].[user_id] and U1.Product_id = Product_id1
Left Join #UserProduct U2 On U2.[user_id] = [User].[user_id] and U2.Product_id = Product_id2
Where (U1.[user_id] Is Not Null And U2.[user_id] Is Not Null)
Group By U1.[user_id]

结果:

enter image description here

答案 4 :(得分:-1)

希望可以帮助您

Select * into #User From (
    Select '1' [user_id], 'john' [user_name] Union All
    Select '2' [user_id], 'mark' [user_name] Union All
    Select '3' [user_id], 'scott' [user_name] Union All
    Select '4' [user_id], 'piter' [user_name] 
) A

Select * into #UserProduct From (
    Select '1' [user_id],  '2' [product_id] union All
    Select '1' [user_id],  '4' [product_id] union All
    Select '1' [user_id],  '5' [product_id] union All
    Select '2' [user_id],  '4' [product_id] union All
    Select '2' [user_id],  '5' [product_id] union All
    Select '2' [user_id],  '7' [product_id] union All
    Select '3' [user_id],  '1' [product_id] union All
    Select '3' [user_id],  '5' [product_id] union All
    Select '3' [user_id],  '4' [product_id] union All
    Select '3' [user_id],  '2' [product_id] union All
    Select '4' [user_id],  '1' [product_id] 
) A


Select U.[user_id], U.[user_name], P.[qty_product]
From #User U
Left Join (
    Select 
        [user_id], Count(*) [qty_product] 
    From #UserProduct
    Group By [user_id]
    Having Count(*) > 1
) P On P.[user_id] = U.[user_id]

结果:

enter image description here