gpt4 book ai didi

sql - 优化的 sql 查询以计算不同表中具有来自同一用户的评论的产品

转载 作者:行者123 更新时间:2023-11-29 12:22:31 25 4
gpt4 key购买 nike

我有 3 个表,每个表都有给定类型产品的评论,即 reviewshirts、reviewcoats、reviewpants所有表都包含用户 ID 和项目 ID 的列。给定一个表中的 itemid,什么是查询其他表中产品组合的优化方法,这些产品组合由使用该 itemid 评论过该项目的用户评论过,并按该组合出现的次数分组。

例如:给定来自 reviewshirts 表的 itemid,'S11111':

表评论衬衫:

------------------------------
| reviewid | itemid | userid |
------------------------------
| ??? | S11111 | U1234 | <---matches
------------------------------
| ??? | S11111 | U4321 | <---matches
------------------------------
| ??? | S99999 | U5555 | (only want userids that reviewed S11111)
------------------------------

表reviewpants:(找到那些用户评论过的所有项目)

------------------------------
| reviewid | itemid | userid |
------------------------------
| ??? | P11111 | U1234 | <---matches
------------------------------
| ??? | P11111 | U4321 | <---matches
------------------------------
| ??? | P11111 | U5555 |
------------------------------
| ??? | P66666 | U4321 | <---matches
------------------------------

表审查外套:

------------------------------
| reviewid | itemid | userid |
------------------------------
| ??? | C11123 | U1234 | <---matches
------------------------------
| ??? | C00024 | U1234 | <---matches
------------------------------
| ??? | C00024 | U4321 | <---matches
------------------------------

返回结果:

---------------------------
| pantid | coatid | count |
---------------------------
| P11111 | C11123 | 1 |
---------------------------
| P11111 | C00024 | 2 |
---------------------------
| P66666 | C00024 | 1 |
---------------------------

(根据评论 S11111 的用户的不同组合的 pantids 和 coatids 的数量对结果进行分组)

感谢您提供的任何帮助!

请求的上下文:这是基于以前评论的简单推荐引擎。

最佳答案

我认为您正在寻找其他两个表格中的成对产品。如果是这样,以下查询似乎就是您要查找的内容:

select rp.pantid, rc.coatid, count(*) as cnt_pairs,
count(distinct rs.userid) as cnt_users
from ReviewShirts rs join
ReviewPants rp
on rs.userid = rp.userid join
ReviewCoats rc
on rs.userid = rc.userid
where rs.itemid = <whatever>
group by rp.pantid, rc.coatid

最后一列,cnt_users,就是你想要的值。

这似乎是一个不寻常的问题。您能否编辑问题以了解如何使用它?

关于sql - 优化的 sql 查询以计算不同表中具有来自同一用户的评论的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11462971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com