如果main_table
的{{1}}空值,我似乎无法获得正确的语法,以便将数据从一个表插入到另一个表中。如果email
的{{1}}电子邮件为空,那么我会尝试从main_table
插入id
的{{1}}:
email
在此示例中,id
中secondary_table
号码MariaDB> SELECT * FROM `main_table`;
+------+---------------------+----------------------------------+------+
| Id | Email | Other | More |
+------+---------------------+----------------------------------+------+
| 1 | user1@somewhere.com | blah | A |
| 2 | | needs email from secondary_table | B |
| 3 | user3@someplace.com | blah | C |
+------+---------------------+----------------------------------+------+
3 rows in set (0.09 sec)
MariaDB> SELECT * FROM `secondary_table`;
+------+---------------------+-------+
| Id | Email | Info |
+------+---------------------+-------+
| 1 | user1@somewhere.com | blah |
| 1 | | blank |
| 2 | user2@something.com | blah |
| 2 | | blank |
| 3 | user3@someplace.com | blah |
| 3 | | blank |
+------+---------------------+-------+
6 rows in set (0.09 sec)
的{{1}}为空id
。我试图将2
从main_table
插入到email
id
。我试过了:
email
还有其他各种各样的失败......也许它很简单,但是我被卡住了!
答案 0 :(得分:1)
查看您的示例似乎您需要更新(使用加入)而不是插入
(假设main_table中的电子邮件字段为空)
update main_table
inner join secondary_table ON main_table.id = secondary_table.id
set main_table.Email = secondary_table.Email
where main_table.Email is null
and secondary_table.email <> ''
或假设在main_table中的电子邮件字段为=&#39;&#39;
update main_table
inner join secondary_table ON main_table.id = secondary_table.id
set main_table.Email = secondary_table.Email
where main_table.Email = ''
and secondary_table.email <> ''