插入选择SQL语句 - 如何在一个语句中从其他两个表获取数据
初学者SQL学生在这里。
我有三个表:
- 客户端
- 帐户
- 在
拥有
中,我有两个列客户端编号
和帐户号
,我想从client
表到第一列的表以及从帐户
表到第二列的所有帐号。
我该如何在一个陈述中做到这一点?
Beginner SQL student here.
I have three tables:
- Client
- Account
- Owns
In Owns
, I have two columns client number
and account number
and I want to add all client numbers from the Client
table to the first column and all account numbers from the Account
table to the second column.
How can I do it in one statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来外部连接。
某些示例数据(当然是虚拟表;只有必要的列):
使用使用
row_number
分析函数的CTE,然后将结果(rn
列)用作加入条件,创建这样的语句:结果是:
请注意,如果
帐户
的行比client
,则必须切换到右JOIN(或保持向左,但请替换表的订单)。Looks like outer join.
Some sample data (dummy tables, of course; only necessary columns):
Using CTEs that utilize
row_number
analytic function whose result (thern
column) is then used as join condition, create such a statement:Result is then:
Note that, if
account
has more rows thanclient
, you'll have to switch to right join (or keep left, but substitute tables' order).