将数据从 Mariadb table 插入另一个 table
Inserting data from Mariadb table into another table
如果 main_table
对 email
的值为空,我似乎无法理解正确的语法以便将数据从一个 table 插入另一个 table。如果 main_table
电子邮件对于 id
是空的,那么我想从 secondary_table
:
中插入 id
的 email
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)
在此示例中,id
数字 2
在 main_table
中 email
为空。我正在尝试将 id
与 email
从 secondary_table
插入到 main_table
。我试过:
INSERT INTO `main_table`
(`Email`)
VALUES
( SELECT `Email` FROM `secondary_table` WHERE `Id` IN
( SELECT `Id` FROM `main_table` WHERE `Email` == '') ) ;
一路上还有其他各种失败...也许这很简单,但我被卡住了!
查看您的示例,您似乎需要更新(通过加入)而不是插入
(假设 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 中的电子邮件字段是 =''
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 <> ''
如果 main_table
对 email
的值为空,我似乎无法理解正确的语法以便将数据从一个 table 插入另一个 table。如果 main_table
电子邮件对于 id
是空的,那么我想从 secondary_table
:
id
的 email
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)
在此示例中,id
数字 2
在 main_table
中 email
为空。我正在尝试将 id
与 email
从 secondary_table
插入到 main_table
。我试过:
INSERT INTO `main_table`
(`Email`)
VALUES
( SELECT `Email` FROM `secondary_table` WHERE `Id` IN
( SELECT `Id` FROM `main_table` WHERE `Email` == '') ) ;
一路上还有其他各种失败...也许这很简单,但我被卡住了!
查看您的示例,您似乎需要更新(通过加入)而不是插入
(假设 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 中的电子邮件字段是 =''
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 <> ''