簡體   English   中英

用於有條件地將數據從兩個表移動到另一個表的 SQL 查詢

[英]SQL Query for conditionally moving data from two tables into another

我有三個表,以下列

Table: raw_data
Columns: first_name, email, club_name

Table: contacts
Columns: first_name, email, club_id

Table: clubs
Columns: club_id, club_name

當前數據存在於 raw_data 表中,我想將數據插入到聯系人表中,如下所示

first_name: (from raw_data)
email: (from raw_data)
club_id: (compare club_name in clubs table and get club_id)

我可以插入姓名和電子郵件數據,但需要幫助進行 club_id 比較

我目前的查詢如下

INSERT INTO contacts (first_name,email)
SELECT first_name,email
FROM raw_data

看起來像一個 JOIN:

INSERT INTO contacts (first_name, email, club_id)
   SELECT r.first_name, r.email, c.club_id
     FROM raw_data r JOIN clubs c ON c.club_name = r.club_name;

暫無
暫無

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

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