[英]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当前数据存在于 raw_data 表中,我想将数据插入到联系人表中,如下所示
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我可以插入姓名和电子邮件数据,但需要帮助进行 club_id 比较
My current query is as follows我目前的查询如下
INSERT INTO contacts (first_name,email)
SELECT first_name,email
FROM raw_data
Looks like a JOIN:看起来像一个 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.