插入选择SQL语句 - 如何在一个语句中从其他两个表获取数据

发布于 2025-02-03 00:02:03 字数 230 浏览 3 评论 0原文

初学者SQL学生在这里。

我有三个表:

  1. 客户端
  2. 帐户
  3. 拥有

中,我有两个列客户端编号帐户号,我想从client表到第一列的表以及从帐户表到第二列的所有帐号。

我该如何在一个陈述中做到这一点?

Beginner SQL student here.

I have three tables:

  1. Client
  2. Account
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2025-02-10 00:02:03

看起来外部连接。

某些示例数据(当然是虚拟表;只有必要的列):

SQL> select * from client;

CLIENT_NUMBER
-------------
            1
            2

SQL> select * from account;

ACCOUNT_NUMBER
--------------
           100

使用使用row_number分析函数的CTE,然后将结果(rn列)用作加入条件,创建这样的语句:

SQL> insert into own (client_number, account_number)
  2  select client_number, account_number
  3  from (with
  4          n_client as
  5            (select client_number,
  6                    row_number() Over (order by client_number) rn
  7             from client
  8            ),
  9          n_account as
 10            (select account_number,
 11                    row_number() over (order by account_number) rn
 12             from account
 13            )
 14          select c.client_number, a.account_number
 15          from n_client c left join n_account a on a.rn = c.rn
 16       );

2 rows created.

结果是:

SQL> select * From own;

CLIENT_NUMBER ACCOUNT_NUMBER
------------- --------------
            1            100
            2

SQL>

请注意,如果帐户的行比client ,则必须切换到右JOIN(或保持向左,但请替换表的订单)。

Looks like outer join.

Some sample data (dummy tables, of course; only necessary columns):

SQL> select * from client;

CLIENT_NUMBER
-------------
            1
            2

SQL> select * from account;

ACCOUNT_NUMBER
--------------
           100

Using CTEs that utilize row_number analytic function whose result (the rn column) is then used as join condition, create such a statement:

SQL> insert into own (client_number, account_number)
  2  select client_number, account_number
  3  from (with
  4          n_client as
  5            (select client_number,
  6                    row_number() Over (order by client_number) rn
  7             from client
  8            ),
  9          n_account as
 10            (select account_number,
 11                    row_number() over (order by account_number) rn
 12             from account
 13            )
 14          select c.client_number, a.account_number
 15          from n_client c left join n_account a on a.rn = c.rn
 16       );

2 rows created.

Result is then:

SQL> select * From own;

CLIENT_NUMBER ACCOUNT_NUMBER
------------- --------------
            1            100
            2

SQL>

Note that, if account has more rows than client, you'll have to switch to right join (or keep left, but substitute tables' order).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文