将选定的列从一个 table 插入到另一个 SQL

Insert selected columns from one table to another SQL

我在同一主机 (localhost:3306) 中有两个独立的数据库。我有一个 table 来自第一个数据库,第二个来自另一个数据库。 这里有2个截图变得更清楚

我需要匹配:

ID -> username
EMAIL -> email
FIRST_NAME -> first_name
LAST_NAME -> last_name

有什么软件可以自动实现吗?我需要编写一些特定的查询来将这 4 列从一个 table 复制到另一个吗? 我在互联网上搜索,但没有找到任何示例。对于信息,第一张图片是来自keycloack的ldap用户,第二张是使用sso auth导入到应用程序的用户。

到现在都是手动复制用户记录

谢谢

您应该尝试使用从 ldap 数据库到应用程序数据库的 missing/different 信息更新用户

update u2 set email=u1.email, first_name=u1.first_name, last_name=u1.last_name
from database2.app_users u2 
join database1.ldap_users u1 on u1.id = u2.username
where isnull(u2.email, '') <> isnull(u1.email, '')
or isnull(u2.first_name, '') <> isnull(u1.first_name, '')
or isnull(u2.last_name, '') <> isnull(u1.last_name, '')

编辑

经过更详细的解释,我了解到您需要插入缺失的用户而不是更新它们。

insert into u2 (username, email, first_name, last_name)
select u1.id, u1.email, u1.first_name, u1.last_name
from database1.ldap_users u1 
left join database2.app_users u2 on u1.id = u2.username
where u2.username is null

这应该有效