使用左连接选择一对多关系中的第一条记录
我正在尝试使用左连接来连接两个表。结果集必须仅包含“正确”连接表中的第一条记录。
假设我有两个表 A 和 B,如下所示;
表“A”
code | emp_no
101 | 12222
102 | 23333
103 | 34444
104 | 45555
105 | 56666
表“B”
code | city | county
101 | Glen Oaks | Queens
101 | Astoria | Queens
101 | Flushing | Queens
102 | Ridgewood | Brooklyn
103 | Bayside | New York
预期输出:
code | emp_no | city | county
101 | 12222 | Glen Oaks | Queens
102 | 23333 | Ridgewood | Brooklyn
103 | 34444 | Bayside | New York
104 | 45555 | NULL | NULL
105 | 56666 | NULL | NULL
如果您发现我的结果只有表“B”中的一条匹配记录(不无论匹配什么记录)在左连接(并且它是一对多映射)之后,
我需要从表 B 中选择第一个匹配的记录并忽略所有其他行。
请帮忙!
谢谢
I'm trying to join two tables using a left-join. And the result set has to include only the first record from the "right" joined table.
Lets say I have two tables A and B as below;
Table "A"
code | emp_no
101 | 12222
102 | 23333
103 | 34444
104 | 45555
105 | 56666
Table "B"
code | city | county
101 | Glen Oaks | Queens
101 | Astoria | Queens
101 | Flushing | Queens
102 | Ridgewood | Brooklyn
103 | Bayside | New York
Expected Output:
code | emp_no | city | county
101 | 12222 | Glen Oaks | Queens
102 | 23333 | Ridgewood | Brooklyn
103 | 34444 | Bayside | New York
104 | 45555 | NULL | NULL
105 | 56666 | NULL | NULL
If you notice my result has only the one matched record from table "B"(doesn't matter what record is matched) after left join (and it is a one to many mapping)
I need to pick the first matched record from table B and ignore all other rows.
Please help!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
经过一番尝试后,发现这比我想象的要棘手!假设 table_b 有一些唯一的单列(例如单字段主键),看起来您可以这样做:
After playing around a bit, this turns out to be trickier than I'd expected! Assuming that
table_b
has some single column that is unique (say, a single-field primary key), it looks like you can do this:另一个选项:
OUTER APPLY
如果数据库支持,
OUTER APPLY
是一个高效且简洁的选项。这会导致左连接到不确定的第一个匹配记录。我的测试表明它比任何其他发布的解决方案(在 MS SQL Server 2012 上)都要快。
Another option:
OUTER APPLY
If supported by the database,
OUTER APPLY
is an efficient and terse option.This results in a left join to the indeterminate first matched record. My tests show it to be quicker than any other posted solution (on MS SQL Server 2012).
得票最高的答案对我来说似乎不正确,而且似乎过于复杂。
只需按子查询中表 B 上的代码字段进行分组,然后选择每个分组的最大 ID。
The highest voted answer does not seem correct to me, and seems overcomplicated.
Just group by the code field on table B in your subquery and select the maximum Id per grouping.
如果您使用的是 SQL Server 2005 或更高版本,则可以使用 排名以达到您想要的效果。特别是,
ROW_NUMBER()
似乎很适合您的需求:或者
If you are on SQL Server 2005 or later version, you could use ranking to achieve what you want. In particular,
ROW_NUMBER()
seems to suit your needs nicely:OR
我修改了 ruakh 的答案,这似乎与 mysql 完美配合。
I modified the answer from ruakh and this seem to work perfectly with mysql.
在 Oracle 中你可以这样做:
In Oracle you can do:
方法如下:
嘿,如果城市和县是唯一的,那么就使用它们
但重点是您必须在其中放置一些表达式来告诉查询处理器它意味着首先 。
this is how:
Hey, if the city and county are unique, then use them
But the point is you have to put some expression in there to tell the query processor what it means to be first.