Oracle左外连接:如何限制右表中的请求
我有一个很大的声明:
SELECT
a.user_id, a.user_name,
s.name, s.value,
d.default
FROM
accounts a,
settings s
LEFT OUTER JOIN default d ON ( d.name = s.name )
WHERE
s.user_id = a.user_id;
问题是 settings
包含大量条目,我需要选择 ID 最高的条目。我可以想象更改语句并用子选择替换连接(它从 settings
获取正确的条目),但我很好奇是否有更好的解决方案。任何意见都会受到赞赏。非常感谢!
I have a large statement:
SELECT
a.user_id, a.user_name,
s.name, s.value,
d.default
FROM
accounts a,
settings s
LEFT OUTER JOIN default d ON ( d.name = s.name )
WHERE
s.user_id = a.user_id;
The problem is that settings
contains a large amount of entries and I need to pick the one with the highest ID. I can imagine do change the statement and replace the join with a subselect (which grabs the correct entry from settings
), but I'm curious to see if there is a better solution. Any input is appreciated. Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用子查询从右表中获取所需的结果。
像这样的东西:
You can use a sub-query to get just the results you want out of the right table.
Something like:
从表中获取最高的 ID 可以使用 a
或 with a
来完成(我更喜欢第一个解决方案)
或者就像约翰提议的那样,但无论如何你都需要一个子查询。
另一件事,您的示例中可能存在一些拼写错误,我不知道 d 来自哪里,也没有进行自动左连接的要点...
这是我会写的
...仍然是子查询
getting the highest ID from a table could be done with a
or with a
(i prefer the first solution)
or just like John proposed, but you'll need a subquery anyway.
One more thing, there might be some typo in your example, i don't see where d is coming from, neither the point of making an auto left join...
Here is what I would have written
... still subquery