簡體   English   中英

如何在 SQL 中將 append 記錄從一個表記錄到另一個表

[英]How to append records from one table to another table in SQL

我有兩個表,第一個表包含 244 列,有 4945 條記錄,而在第二個表中,有 11 列有 3737 條記錄,但是 id、name、tocken、tockenold 中共有 4 列。 如何組合這兩個表

我嘗試使用 UNION,但兩者的列必須相等。 嘗試使用完全連接獲取確切的列和記錄計數,但第二個表記錄為空。

誰能幫我解決這個問題。

嘗試這個:

select *
  from (
        select id, name, tocken, tockenold
          from table_1
         union 
        select id, name, tocken, tockenold
          from table_2
       ) m
  join table_1 t1
    on m.id = t1.id and m.name = t1.name and m.tocken = t1.tocken and m.tockenold = t1.tockenold 
  join table_2 t2
    on m.id = t2.id and m.name = t2.name and m.tocken = t2.tocken and m.tockenold = t2.tockenold  

根據你說的:

我需要得到 4945+3737 = 8682 條記錄和 251 列

聽起來您只需要在兩個表之間進行手動聯合:

SELECT 
  t1_c1, t1_c2, ..., t1_c240, 
  common_c1, common_c2, common_c3, common_c4,
  null, null, null, null, null, null, null
FROM table1

UNION ALL

SELECT 
  null, null, .... null, -- 240 times
  common_c1, common_c2, common_c3, common_c4,
  t2_c1, t2_c2, ..., t2_c7
FROM table2

暫無
暫無

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

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