繁体   English   中英

一个表中的MYSQL SUM()并与其他2个表中的JOIN

[英]MYSQL SUM() in one table and JOIN with 2 other tables

我有3个表格: itemspurchasescollaborators 用户可以拥有商品,购买商品或成为商品的协作者。 此外,所购买的商品可以评级为+1-1 所有者或合作者无法购买自己的物品。

我想获取给定用户的所有商品,并显示每个商品的评分。

这是我的桌子:

      items           |           purchases           |     collaborators
i_id  item_id user_id | p_id item_id  user_id  rating |c_id item_id user_id
  1      1       11   |  1      1         13     -1   |  1     1        12 
  2      2       12   |  2      2         11      1   |  2     2        13
  3      3       13   |  3      3         12     NULL |    
                      |  4      1         14     -1   |

到目前为止,这是我的MYSQL查询:

select *, count(p_id) as tots, sum(rating=1) as yes, sum(rating= '-1') as no
from items
left join purchases
on items.item_id=purchases.item_id
left join collaborators
on items.item_id=collaborators.item_id
where items.user_id=13 or purchases.user_id=13 or collaborators.user_id=13
group by items.item_id

这是我对user_id=11预期结果(更改WHERE子句中的每个 user_id ):

item_id    tots  yes no
  1         2     0   2
  2         1     1   0
// notice how user_id=11 doesn't have anything to do with item_id=3

这是我对user_id=12预期结果:

item_id    tots  yes no
  1         2     0   2
  2         1     1   0    
  3         1     1   0  

这是我对user_id=13预期结果:

item_id    tots  yes no
  1         2     0   2
  2         1     1   0    
  3         1     1   0   
//notice user_id=13 should have same results as user_id=12. Although, their 
relation to each of the 3 items is different, they still either purchased, 
own, or collaboratored on each of them.

不幸的是,我得到前两个结果,但对于user_id=13却没有正确的结果。 对于user_id=13, item_id=1tots=1而不是tots=2出于某些我无法理解的原因)。

我们将不胜感激任何想法,例如“最好将此分为两个查询”,

我仍然不太确定我是否理解您的正确,但是您可以尝试按照以下说明进行操作,然后让我们从那里开始。

编辑

以下语句返回预期结果。
您可以在此处验证(使用SQL Server)

其要旨是

  • 从三个表中选择所有可能的user_id和item_id组合
  • 选择每个项目的计数/评分
  • 结合结果

SQL语句

SELECT u.user_id, pt.item_id, pt.cnt, pt.yes, pt.no
FROM   (
        SELECT user_id, item_id, title FROM items 
        UNION SELECT user_id, item_id, NULL FROM purchases
        UNION SELECT user_id, item_id, NULL FROM collaborators      
       ) u INNER JOIN (
        SELECT COUNT(*) AS cnt
               , SUM(CASE WHEN ISNULL(rating, 1) = 1 THEN 1 ELSE 0 END) AS yes
               , SUM(CASE WHEN rating =-1 THEN 1 ELSE 0 END) AS no
               , item_id
        FROM   purchases
        GROUP BY
               item_id
      ) pt ON pt.item_id = u.item_id    

MYSQL陈述式

SELECT u.user_id, pt.item_id, pt.cnt, pt.yes, pt.no, u.title
FROM   (
    SELECT user_id, item_id, title FROM items where user_id=13
    UNION SELECT user_id, item_id, NULL FROM purchases where user_id=13
    UNION SELECT user_id, item_id, NULL FROM collaborators where user_id=13       
   ) u INNER JOIN (
    SELECT COUNT(*) AS cnt
           , SUM(rating=1) AS yes
           , SUM(rating =-1) AS no
           , item_id
    FROM   purchases 
    GROUP BY
           item_id
  ) pt ON pt.item_id = u.item_id 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM