如何在Oracle中插入数据?

发布于 2025-01-25 09:15:38 字数 380 浏览 2 评论 0原文

我有一个表T1。 根据另一个表T2,T3等的查询结果将数据插入该表中

我想

Insert into table_t1 (column1,column2,column3,column4)
Select col1, col2, Have_to_check_condition_here_and_then_insert_value, Have_to_check_condition_here_and_then_insert_value from table_t2
where condition;

。而且,这也是有条件地,这意味着如果某事是正确的,那么我将把value_something放在否则value_another_thing。表3和第4位与特定列上的第二个表连接。

I've a table t1. And I want to insert data into this table based on query result of another tables t2, t3 etc.

I'm doing it like this-

Insert into table_t1 (column1,column2,column3,column4)
Select col1, col2, Have_to_check_condition_here_and_then_insert_value, Have_to_check_condition_here_and_then_insert_value from table_t2
where condition;

The problem I'm facing is how to put values inside column3 and column4 which comes from table_t3 and table_t4 and that too conditionally, it means if something is true then I'll put value_something otherwise value_another_thing. Table 3rd and 4th are connected with 2nd table on a particular column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倒数 2025-02-01 09:15:38

有了您发布的信息,这将是这样的:

INSERT INTO table_t1 (col1,
                      col2,
                      col3,
                      col4)
   SELECT b.col1,
          c.col2,
          CASE
             WHEN b.amount > 1000 THEN c.one_value
             ELSE c.some_other_value
          END AS col3,
          --
          CASE
             WHEN c.TYPE = 'A' THEN b.some_column
             WHEN c.TYPE IN ('B', 'C') THEN c.another_column
             ELSE NULL
          END AS col4
   FROM   table_t3 b JOIN table_t4 c ON b.id = c.id

With as much info as you posted, that would be something like this:

INSERT INTO table_t1 (col1,
                      col2,
                      col3,
                      col4)
   SELECT b.col1,
          c.col2,
          CASE
             WHEN b.amount > 1000 THEN c.one_value
             ELSE c.some_other_value
          END AS col3,
          --
          CASE
             WHEN c.TYPE = 'A' THEN b.some_column
             WHEN c.TYPE IN ('B', 'C') THEN c.another_column
             ELSE NULL
          END AS col4
   FROM   table_t3 b JOIN table_t4 c ON b.id = c.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文