簡體   English   中英

如何使用內部聯接從兩個表中 select 數據並直接插入第三個表?

[英]How can I select data from two tables using an inner join and insert directly into a third table?

我有兩個帶有模式的表,如下所示:

user.table1
pid       => varchar
timestamp => timestamp
sid       => varchar

pid  timestamp    sid  attribute1  attribute2  ...
-------------------------------------------------------------------
1    2020/01/20   10   ...         ...         ...
2    2020/02/28   10   ...         ...         ...
3    2020/03/01   10   ...         ...         ...
4    2020/04/08   20   ...         ...         ...
5    2020/05/31   20   ...         ...         ...
6    2020/06/30   20   ...         ...         ...
7    2020/06/31   20   ...         ...         ...
8    2020/07/31   20   ...         ...         ...
user.table2
pid  => varchar
text => blob

pid  text
-------------------------------------------------------------------
1    xxx
2    abc
3    def
4    yyy
5    sss
6    abc
7    rrr
8    fff

我需要創建第三個表,其中應包含以下信息:

user.table3
pid  timestamp    sid  text
-------------------------------
1    2020/01/20   10   xxx     
2    2020/02/28   10   abc     
3    2020/03/01   10   def     
4    2020/04/08   20   yyy     
5    2020/05/31   20   sss     
6    2020/06/30   20   abc     
7    2020/06/31   20   rrr     
8    2020/07/31   20   fff     

知道如何在一個 SQL 語句中執行 select、內連接和插入嗎?

我想在一個 SQL 語句中執行此操作的原因是我不想將信息從數據庫中提取到 Java 中,然后將其寫回數據庫。 我之前做過后者,但運行速度非常慢。

目前,即使我在下面寫的 select 也不起作用。

select 
  table1.pid, table1.sid, table1.timestamp, utl_raw.cast_to_varchar2(dbms_lob.substr(table2.text,10,1)) 
from user.table1 inner join user.table2 
using pid 
where pid in ('1', '2');

只需將您的 select 變成插入語句

INSERT INTO USER.table3(pid, sid, timestamp, text)
    SELECT t1.pid,
           t1.sid,
           t1.timestamp,
           UTL_RAW.cast_to_varchar2 (DBMS_LOB.SUBSTR (t2.text, 10, 1))
      FROM USER.table1 t1 INNER JOIN USER.table2 t2 ON (t1.pid = t2.pid)
     WHERE t1.pid IN ('1', '2');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM