简体   繁体   English

SQL查询有条件地联接来自不同数据库的列

[英]SQL Query to conditionally join columns from different databases

I am migrating data from an old DB to a new one. 我正在将数据从旧数据库迁移到新数据库。 My goal is to move a user's permissions from the old DB to the new DB. 我的目标是将用户的权限从旧数据库移到新数据库。 Both the old and new DBs already have matching user IDs. 新旧数据库都已经具有匹配的用户ID。

The problem is that the old database has multiple rows for each user's permissions( Ex. User #3 might have 5 rows with different sets of permissions based on the organization id in the table's second column. 问题在于旧数据库对每个用户的权限都有多行(例如,根据表第二列中的组织ID,用户#3可能有5行具有不同的权限集。

How can I: 我怎样才能:

Populate the new table with the user's highest permission only for each ('Admin' being the highest, 'Read' permission being the lowest authority) 仅使用每个用户的最高权限填充新表(“ Admin”是最高权限,“ Read”权限是最低权限)

OLD_DB USER TABLE: OLD_DB USER TABLE:

ID     ORG     ADMIN   EDIT    READ
1       43       0       1      0
1       200      1       0      0
1       76       0       0      1
2       43       0       1      0
2       76       0       0      1

NEW_DB USER TABLE: NEW_DB用户表:

ID     LEVEL
1        0
2        0
3        0
4        0
5        0

NEW_DB USER TABLE GOAL: NEW_DB用户表目标:

ID     LEVEL
1      ADMIN
2      EDIT
3      READ
4      READ
5      EDIT

I have no interest in the user 'ORG' relation anymore and just want their highest permission level. 我对用户的“ ORG”关系不再感兴趣,只希望获得他们的最高权限。

I am using Sequel Pro, which may offer a solution in it's GUI, but i'm not aware of it. 我正在使用Sequel Pro,它可能会在GUI中提供解决方案,但我不知道。 My latest attempt in code to just obtain the admin users (with no luck) was: 我最近在代码中仅获取管理员用户(没有运气)的尝试是:

SELECT admin FROM old_db.user oldpermission
JOIN scope FROM homestead_test.users newpermission
ON oldpermission.ID = newpermission.ID;
 SELECT ID, CASE WHEN MAX(ADMIN) = 1 THEN 'ADMIN'
                 WHEN MAX(EDIT)  = 1 THEN 'EDIT'
                 WHEN MAX(READ)  = 1 THEN 'READ'
            END as LEVEL
 FROM OLD_DB 
 GROUP BY ID

You probably can simplify the last one to 您可能可以简化最后一个

 ELSE 'READ'

Because, if fail first 2 then only READ permission is available 因为,如果前两个失败,那么只有READ权限可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM