优化的SQL查询来计算产品中具有来自同一用户
问题描述:
我有3个表,每一个给定类型的产品评论的评论不同的表, 即reviewshirts,reviewcoats,reviewpants 所有表的列的用户ID,和一个itemid。 给定一个表中的itemid,查询其他表中的产品组合的优化方式是什么,用户使用该itemid检查了该项目,并根据组合出现的次数进行分组,从而进行查询。优化的SQL查询来计算产品中具有来自同一用户
例如: 给出从reviewshirts表, 'S11111' 的itemid的:
表reviewshirts:
------------------------------
| 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
------------------------------
表reviewcoats:
------------------------------
| reviewid | itemid | userid |
------------------------------
| ??? | C11123 | U1234 | <---matches
------------------------------
| ??? | C00024 | U1234 | <---matches
------------------------------
| ??? | C00024 | U4321 | <---matches
------------------------------
返回结果:
---------------------------
| pantid | coatid | count |
---------------------------
| P11111 | C11123 | 1 |
---------------------------
| P11111 | C00024 | 2 |
---------------------------
| P66666 | C00024 | 1 |
---------------------------
(由pantids和coatids的不同组合的组数从结果谁审查S11111用户)
感谢您的帮助,您可以提供!
上下文请求: 这是基于以前的评论一个天真的推荐引擎。
答
我想你正在寻找其他两个表中的产品对。如果是这样,下面的查询似乎是你在找什么:
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,是你想要的值。
这似乎是一个不寻常的问题。你可以编辑这个问题来给出一些想法如何使用?
+0
这非常有效!谢谢! – zenithius 2012-07-13 02:25:27
你在找裤子和大衣之间的搭配吗?或者,你是否在寻找与裤子和内衣搭配的组合? – 2012-07-13 01:42:37
我正在寻找裤子和大衣的组合,基于衬衫表中的用户名。 – zenithius 2012-07-13 01:55:47
。 。这是我的理解,我只是想确定。这是用于某种市场购物篮分析或推荐引擎吗? – 2012-07-13 02:04:48