如何 select 对至少有 3 个产品评分至少为 4/5 的用户 ID sql

How to select pair of userID that have at least 3 reviewed productID with a product score of at least 4/5 in sql

我有这样的数据集:

userid productid score
A 1 4
A 2 4
A 3 5
B 1 4
B 2 4
B 3 5

我想要这样的输出:

userid1 userid2 matching_product
A B 1 2 3

但我只能通过此查询获得前两列:

CREATE TABLE score_greater_than_3 AS
SELECT userid, productid, score
FROM reviews
WHERE score >= 4;

SELECT s1.userid as userid1, s2.userid as userid2
FROM score_greater_than_3 s1 
INNER JOIN score_greater_than_3 s2 ON s1.productid=s2.productid AND s1.userid<s2.userid
GROUP BY s1.userid, s2.userid
HAVING count(*)>=3;

如何获得匹配的产品?如果更简单的话,我也可以接受这样的输出

user1 user2 matched product
a b 1
a b 2
a b 3

请试试这个:

select s1.userid as userid1, s2.userid as userid2, 
GROUP_CONCAT(s1.productid) 
from score_greater_than_3 s1 inner join score_greater_than_3 s2 on s1.productid=s2.productid and s1.userid<s2.userid
group by s1.userid, s2.userid;

完整脚本请看:DB Fiddle

您可以尝试以下查询:

WITH cte AS (
    SELECT r.userid,
           r.productid
    FROM reviews r
    WHERE r.score > 3
)
SELECT r1.userid,
       r2.userid,
       GROUP_CONCAT(r1.productid SEPARATOR ' ')       
FROM       cte r1
INNER JOIN cte r2
        ON r1.productid = r2.productid
       AND r1.userid < r2.userid
GROUP BY r1.userid,
         r2.userid
HAVING COUNT(*) >= 3

它使用 Common Table Expression that allows you to allocate your table in a temporary space that lasts till the end of the query. Your next step using the self join is correct, though it lacks the GROUP_CONCAT 聚合函数,允许您在 string-like 字段上进行聚合。您可以设置“分隔符”参数来决定要使用哪个字符串来连接您的值。

尝试完整查询 here