在 Postgres SELECT 语句中选择另一个表中的行数
我不太知道如何表达这个,所以也请帮我写一下标题。 :)
我有两张桌子。我们将它们称为 A
和 B
。 B
表有一个指向 A.id
的 a_id
外键。现在我想编写一个 SELECT
语句来获取所有 A
记录,并附加一个列,其中包含每个
行。B
记录的计数结果集中的每一行都有一个
我现在正在使用 Postgresql 9,但我想这将是一个通用的 SQL 问题?
编辑:
最后我选择了触发器缓存解决方案,其中每次 B
更改时都会通过函数更新 A.b_count 。
I don't know quite how to phrase this so please help me with the title as well. :)
I have two tables. Let's call them A
and B
. The B
table has a a_id
foreign key that points at A.id
. Now I would like to write a SELECT
statement that fetches all A
records, with an additional column containing the count of B
records per A
row for each row in the result set.
I'm using Postgresql 9 right now, but I guess this would be a generic SQL question?
EDIT:
In the end I went for trigger-cache solution, where A.b_count
is updated via a function each time B
changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为 @intgr 在另一个答案中的评论非常有价值,我将其作为替代答案提出,因为此方法允许您有效地过滤计算列。
I think the comment by @intgr in another answer is so valuable I'm putting forward this as an alternate answer as this method allows you to filter the calculated column efficiently.
上面给出的子查询解决方案效率很低。触发器解决方案可能在大多数读取的数据库中是最好的,但为了记录,这里有一种连接方法,其性能比子查询更好:
如果您使用 Django ORM,您可以简单地编写:
The subquery solution given above is inefficient. The trigger solution is probably best in a mostly-read database, but for the record here's a join approach that will perform better than a subquery:
If you're using Django ORM you can simply write:
根据我的测试,接受的答案效率低下(慢)。表 B 的子查询对表 A 的每一行执行。我使用以下基于分组和联接的方法。它的工作速度更快:
另一种变体:
Accepted answer is inefficient (slow) based on my tests. The subquery of table B executing for every row of table A. I'm using following approach based on grouping and joining. It works much faster:
Another variant:
回答我自己的问题:
To answer my own question:
虽然子查询的效率可能较低,但效率降低的程度取决于用例。另一件需要考虑的事情是正在使用的过滤器。
我有一个“批准者”表 A
我有一个“审批任务”表 B,
我想显示所有审批者的列表以及他们拥有的活动审批任务的计数。现在,我对 SQL 的了解有限,但无论我尝试使用不同类型的联接,我的批准者列表都是不完整的。为什么?我需要在表 B 上有一个过滤器,以便只返回活动任务。如果审批者只有非活动/已完成的任务,则没有计数。这应该显示 0,但由于某种原因它根本不显示该行。
所以,我使用了子查询,它工作得很好。
Whilst a sub-query may be less efficient, how much less efficient depends on the use-case. Another thing to consider is the filters that are being used.
I have a Table A of "Approvers"
I have a Table B of "Approval tasks"
I want to show a list of ALL approvers along with a count of how many ACTIVE approval tasks they have. Now, my knowledge of SQL is limited, but no matter what I tried with the different types of join, my list of approvers was incomplete. Why? I need to have a filter on table B so that only active tasks are returned. If an approver only has inactive/complete tasks, there is no count. This should show 0, but for some reason it just doesn't show the row at all.
So, I use a sub-query and it works perfectly.