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

时间:2012-07-13 01:29:41

标签: sql postgresql postgresql-9.1

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

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

表reviewhirts:

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

表审核人:(查找由这些用户审核的所有项目)

------------------------------
| 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的不同组合数量进行分组)

感谢您提供的任何帮助!

请求的上下文: 这是针对以前评论的天真推荐引擎。

1 个答案:

答案 0 :(得分:1)

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

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是您想要的值。

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