简体   繁体   中英

SQL Query for conditionally moving data from two tables into another

I have three tables, with following columns

Table: raw_data
Columns: first_name, email, club_name

Table: contacts
Columns: first_name, email, club_id

Table: clubs
Columns: club_id, club_name

Currently Data is present in raw_data table, I want to insert data into contacts table as follows

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

I am able to insert name and email data but need help for club_id comparison

My current query is as follows

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

Looks like a 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM