如何外连接两个表(主表和多对一子表)以仅从第二个表中获取一项?
我有两个类似这样的表:
主表:id (int)、title (varchar) 等。 子表:main_table_id(进入主表的外键)、标签(varchar)等。
主表中的给定行可以有零个或多个子表行。
我想做一个查询,该查询将返回主表的每一行,以及主表的列,以及子表中仅一行(与哪一行无关)的列(如果有) ,否则这些列中为 NULL。
当然,如果我只是执行基本的 LEFT OUTER JOIN,那么我当然会多次重复主表,子表中的每个匹配项都会重复一次。
我确信在使用 LEFT OUTER JOIN 和某种强制从子表中仅选择一行而不是全部行的技巧之前,我已经看到过这种做法——也许会选出最小或最大的 OID。 然而,一个多小时的谷歌搜索并没有给出任何解决方案。
有人有这个技巧吗?
I have two tables that are something like this:
Main table: id (int), title (varchar), etc.
Sub-table: main_table_id (foreign key into main table), tag (varchar), etc.
There can be zero or more subtable rows for a given row in the main table.
I want to do a query that will return every row of the main table, with the columns of the main table, and a the columns from only a single row (doesn't matter which) of the sub-table, if there are any, otherwise NULL in those columns.
Of course if I just do a basic LEFT OUTER JOIN then of course I get the main table repeated multiple times, one for each match in the sub-table.
I'm sure I have seen this done before using a LEFT OUTER JOIN and some sort of trickery that forces only one row to be selected from the sub-table, not all of them -- maybe picking out the minimum or maximum OID. However, more than an hour of googling has not yielded any solutions.
Does anybody have this trick in their toolbelt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 SQL Server,则可以使用 TOP 子句。 如果它不是 SQL Server,您必须查看该数据库是否提供同等的功能(许多数据库都提供)。 像这样的东西...
注意:这是为了向您展示总体思路。 我没有机会运行它,因此可能需要进行一些更改,但概念已经存在。
If you're using SQL Server, you can use the TOP clause. If it is something other than SQL Server, you'll have to see if that database offers something equivalent (many of them do). Something like this ...
Note: That is to show you the general idea. I didn't have a chance to run it, so there might be a couple of changes necessary, but the concept is there.
我最喜欢查理的答案,但我不确定 Postges 是否有 TOP/LIMIT 函数,所以这里有一个不需要的替代解决方案(但假设 sub_table 有一个名为“id”的主键):
I like Charlie's answer best, but i'm not sure if Postges has a TOP/LIMIT function, so here's an alternate solution that doesn't need one (but does assume sub_table has a primary key called "id"):
如果您只是检查第二个表中是否有与第一个表中的项目相关的内容,您可以使用带有 group by 子句和聚合的外连接:
然后,t2count 为 0 的任何内容都将是那些table2 中没有任何内容
编辑: 实际上,我不记得 t2count 中是否有 null 或 0...它应该是这些值之一。
If you're just checking of there is something in the second table that goes with the items in the first table you can use an outer join with a group by clause and an aggregate:
Then, anything that has 0 for t2count will be those that have nothing in table2
edit: actually, I don't remember if t2count will have null or 0 in it...it should be one of those values.
Postgres 中有趣的方法(根本不是学校的):
解释:
您选择完整的记录作为一个字段:
您将其转换为字符变化,以便能够使用 max aggregat(或 min ...)
您仅通过 main_table_id 获得一行,其中包含代表您的记录的 varchar,因此我们必须再次转换“子表”并爆炸它 -> (myvarchar::subtable).*
您可以通过 main_table_id 获得格式良好的表子表,只需一行
Funny method in Postgres (not schooll at all) :
Explanation :
You select the complete record as one field :
You cast this as character varying to be able to use max aggregat (or min ...)
You obtain only one line by main_table_id containing a varchar representing your record, so we have to cast again in "subtable" and explode it -> (myvarchar::subtable).*
You obtain your table subtable well formed with just one line by main_table_id