需要查询帮助——聚合和多重连接
我的应用程序中使用了几个表。一个维护产品列表,另一个维护对这些项目的评论,另一个包含这些项目的星级评级,最后一个包含这些项目的购买情况。我的表看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
使用
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.这在一定程度上取决于你使用的数据库,但这在 PostgreSQL 中不起作用:
It'll depend somewhat on what db you're using, but this outta work in PostgreSQL: