SQL 查询有条件地将数据从两个表移动到另一个表
SQL Query for conditionally moving data from two tables into another
我有三个 table,其中包含以下列
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 table,我想将数据插入联系人 table,如下所示
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;
我有三个 table,其中包含以下列
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 table,我想将数据插入联系人 table,如下所示
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;