[英]MYSQL SUM() in one table and JOIN with 2 other tables
I have 3 tables: items
, purchases
, and collaborators
. 我有3个表格: items
, purchases
和collaborators
。 A user can own an item, purchase an item, or be a collaborator on an item. 用户可以拥有商品,购买商品或成为商品的协作者。 Additionally, items that are purchased can be rated up, +1
, or down, -1
. 此外,所购买的商品可以评级为+1
或-1
。 An owner or collaborator can't purchase their own item. 所有者或合作者无法购买自己的物品。
I'd like to get all items for a given user and also display the ratings on each item. 我想获取给定用户的所有商品,并显示每个商品的评分。
Here's my tables: 这是我的桌子:
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 |
Here's my MYSQL query so far: 到目前为止,这是我的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
Here's my expected results for user_id=11
(changing each user_id
in the WHERE
clause): 这是我对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
Here's my expected results for user_id=12
: 这是我对user_id=12
预期结果:
item_id tots yes no
1 2 0 2
2 1 1 0
3 1 1 0
Here's my expected results for user_id=13
: 这是我对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.
Unfortunately, I get the first two results but not the correct one for user_id=13
. 不幸的是,我得到前两个结果,但对于user_id=13
却没有正确的结果。 For user_id=13, item_id=1
the tots=1
and not tots=2
for some reason I can't understand. 对于user_id=13, item_id=1
, tots=1
而不是tots=2
出于某些我无法理解的原因)。
Any thoughts, such as, "its better to separate this into 2 queries", would be greatly appreciated, 我们将不胜感激任何想法,例如“最好将此分为两个查询”,
I'm still not entirly sure I understand you correct but you could try following statement and let us work from there. 我仍然不太确定我是否理解您的正确,但是您可以尝试按照以下说明进行操作,然后让我们从那里开始。
Edit 编辑
Following statement returns the expected results. 以下语句返回预期结果。
You can verify this (using SQL Server) here . 您可以在此处验证(使用SQL Server) 。
The gist of this is to 其要旨是
SQL Statement 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 statement 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.