如何查询有序哈希表 (PostgreSQL)
我有一个哈希表(用于标记项目),其中有一个额外的列,用于确定项目与其标签之间的关系强度:
item_id | tag_name | relationship
1 | linux | 0.7
1 | computer | 0.9
2 | garden | 0.5
2 | shovel | 0.65
...
我想选择所有标记有 'linux'
和 ' 的项目computer'
(允许更多标签),按项目与这两个标签之间的关系之和排序。
这是我查询数据库的尝试:
SELECT * FROM items
INNER JOIN (
SELECT items.id FROM items
INNER JOIN tags ON items.id = tags.item_id
GROUP BY tags.item_id
HAVING ARRAY_AGG(tags.tag_id) @> ARRAY['linux','computer']
ORDER BY SUM(tags.relationship) DESC
) tagged_items ON items.id = tagged_items.id
对于 only 标记有 'linux'
和 'computer'
的项目,这非常有效。但问题是,对于具有附加标签的项目,ORDER BY SUM(tags.relationship)
将对项目所具有的其他标签进行求和...换句话说,具有以下标签的项目大多数标签将首先返回(这不是我想要的)。
如何使 SUM()
仅对与我选择的标签的关系求和?
I have a hash table (for tagging items) with an extra column for the strength of the relationship between items and their tags:
item_id | tag_name | relationship
1 | linux | 0.7
1 | computer | 0.9
2 | garden | 0.5
2 | shovel | 0.65
...
I want to select all items tagged with both 'linux'
and 'computer'
(more tags are allowed), ordered by the sum of the relationships between items and these two tags.
Here's my attempt at querying the database:
SELECT * FROM items
INNER JOIN (
SELECT items.id FROM items
INNER JOIN tags ON items.id = tags.item_id
GROUP BY tags.item_id
HAVING ARRAY_AGG(tags.tag_id) @> ARRAY['linux','computer']
ORDER BY SUM(tags.relationship) DESC
) tagged_items ON items.id = tagged_items.id
For items that are only tagged with 'linux'
and 'computer'
, this works perfectly. But the problem is that for items that have additional tags, the ORDER BY SUM(tags.relationship)
will sum over the other tags that an item has as well... in other words, items with the most tags will return first (and that's not what I want).
How can I make SUM()
only sum over the relationships to the tags that I'm selecting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地使用
in
:除非您想要所有列,否则您可以在 cte 和 join 中使用上面的查询:
You can simply use
in
:Unless you want all columns, then you can use the query above in cte and join: