需要查询帮助——聚合和多重连接

发布于 2024-11-17 04:42:56 字数 1280 浏览 5 评论 0原文

我的应用程序中使用了几个表。一个维护产品列表,另一个维护对这些项目的评论,另一个包含这些项目的星级评级,最后一个包含这些项目的购买情况。我的表看起来像这样:

tbl_item:
---------
id      INT (primary key)
name    VARCHAR (product name)

tbl_comment:
------------
id          INT (primary key)
item_id     INT (foregin key -> tbl_item.id)
commenttext VARCHAR

tbl_rating:
-----------
id          INT (primary key)
item_id     INT (foreign key -> tbl_item.id)
rating      DOUBLE

tbl_purchases:
--------------
id          INT (primary key)
item_id     INT (foreign key -> tbl_item.id)

我想执行一个返回以下内容的查询:

* The design ID
* The average rating
* The number of comments
* The number of purchases

我有与此类似的内容,但它返回不正确的数据:

SELECT  d.id ,
        COUNT(tbl_purchases.id) AS purchase_count, 
        COUNT(tbl_comment.id) AS comment_count,
        AVG(tbl_rating.rating) AS item_rating,
    FROM tbl_item d
    LEFT JOIN tbl_purchases ON tbl_purchases.item_id = d.id
    LEFT JOIN tbl_comment ON tbl_comment.item_id = d.id
    LEFT JOIN tbl_rating ON tbl_rating.id = d.id
    GROUP BY d.id;

我发现我的 COUNT() 列返回相同的值两列,这绝对是不正确的。显然我在连接或 GROUP BY 中做错了什么,但我不完全确定是什么。我是一个 Java 人员,而不是 SQL 人员,所以我不确定这个 SELECT 语句出了什么问题。

谁能帮助我构建这个查询?有没有办法以这种方式跨多个不同的表执行聚合查询?谢谢!!

I have several tables that are used in my application. One maintains a list of products, another maintains comments on those items, another contains star ratings for those items, and the last has the purchases of those items. My tables look something like this:

tbl_item:
---------
id      INT (primary key)
name    VARCHAR (product name)

tbl_comment:
------------
id          INT (primary key)
item_id     INT (foregin key -> tbl_item.id)
commenttext VARCHAR

tbl_rating:
-----------
id          INT (primary key)
item_id     INT (foreign key -> tbl_item.id)
rating      DOUBLE

tbl_purchases:
--------------
id          INT (primary key)
item_id     INT (foreign key -> tbl_item.id)

I would like to execute a query that returns the following:

* The design ID
* The average rating
* The number of comments
* The number of purchases

I had something similar to this, but it returns the incorrect data:

SELECT  d.id ,
        COUNT(tbl_purchases.id) AS purchase_count, 
        COUNT(tbl_comment.id) AS comment_count,
        AVG(tbl_rating.rating) AS item_rating,
    FROM tbl_item d
    LEFT JOIN tbl_purchases ON tbl_purchases.item_id = d.id
    LEFT JOIN tbl_comment ON tbl_comment.item_id = d.id
    LEFT JOIN tbl_rating ON tbl_rating.id = d.id
    GROUP BY d.id;

What I've found is that my COUNT() columns return the same value for both columns, which is definitely not correct. Clearly I'm doing something wrong in my joins or my GROUP BY, but I'm not entirely sure what. I'm a Java guy, not a SQL guy, so I'm not sure what's going wrong in this SELECT statement.

Can anyone give me a hand in constructing this query? Is there a way to perform this aggregate query across several different tables this way? Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪落纷纷 2024-11-24 04:42:56

试试这个:

SELECT  d.id ,
        COALESCE(t.purchase_count,0) as purchase_count, 
        COALESCE(c.comment_count,0) as comment_count,
        r.item_rating,
    FROM tbl_item d
    LEFT JOIN (SELECT item_id, COUNT(1) as purchase_count from tbl_purchases group by item_id) as t on t.item_id = d.id
    LEFT JOIN (SELECT item_id, COUNT(1) as comment_count from tbl_comment group by item_id) as c ON c.item_id = d.id
    LEFT JOIN (SELECT item_id, AVG(rating) as item_rating from tbl_rating group by item_id) as r ON r.item_id = d.id;

Try this:

SELECT  d.id ,
        COALESCE(t.purchase_count,0) as purchase_count, 
        COALESCE(c.comment_count,0) as comment_count,
        r.item_rating,
    FROM tbl_item d
    LEFT JOIN (SELECT item_id, COUNT(1) as purchase_count from tbl_purchases group by item_id) as t on t.item_id = d.id
    LEFT JOIN (SELECT item_id, COUNT(1) as comment_count from tbl_comment group by item_id) as c ON c.item_id = d.id
    LEFT JOIN (SELECT item_id, AVG(rating) as item_rating from tbl_rating group by item_id) as r ON r.item_id = d.id;
伴梦长久 2024-11-24 04:42:56

使用 count(distinct(tbl_purchases.id)) 应该可以解决您的问题,而无需其他人提供的更复杂的查询(但也是正确的)查询。

Using count(distinct(tbl_purchases.id)) should resolve your problem without the more complex queries (but also correct) queries others have offered.

花辞树 2024-11-24 04:42:56

这在一定程度上取决于你使用的数据库,但这在 PostgreSQL 中不起作用:

    SELECT d.id , p.count, c.count, AVG(I.rating)
      FROM tbl_item d
      JOIN ( SELECT count(id), item_id as id from tbl_purchases ) as P
     USING (id)
      JOIN ( SELECT count(id), item_id as id from tbl_comment ) as C
     USING (id)
 LEFT JOIN tbl_rating as I
        ON tbl_rating.id = d.id
  GROUP BY d.id
;

It'll depend somewhat on what db you're using, but this outta work in PostgreSQL:

    SELECT d.id , p.count, c.count, AVG(I.rating)
      FROM tbl_item d
      JOIN ( SELECT count(id), item_id as id from tbl_purchases ) as P
     USING (id)
      JOIN ( SELECT count(id), item_id as id from tbl_comment ) as C
     USING (id)
 LEFT JOIN tbl_rating as I
        ON tbl_rating.id = d.id
  GROUP BY d.id
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文