您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

一列的SQL Server唯一联合

一列的SQL Server唯一联合

您不需要工会。只需离开工作室即可加入项目,然后使用合并

以下

WITH g_studio_UsersInRole AS  --Sampledata
(
    SELECT 1  userId        ,1       roleId   , 'TRUE' value
    UNION SELECT 1,       2,       'TRUE'
    UNION SELECT 1,       3,       'TRUE'
    UNION SELECT 2,       1,       'FALSE')
, g_project_UsersInRole as --Sampledata
(
    SELECT 1 userId  ,       2  roleId  ,     'FALSE'  value ,     1 projectId
    UNION SELECT 2,       1,       'TRUE',     1
)

SELECT 
    sRole.userId,
    sRole.roleId,
    COALESCE(pRole.Value,sRole.value)  as value
FROM 
    g_studio_UsersInRole sRole
    LEFT JOIN g_project_UsersInRole  pRole
    ON sRole.userId = pRole.userId
      and sRole.roleId = pRole.roleId
     and pRole.ProjectId = 1

返回以下结果

userId      roleId      value
----------- ----------- -----
1           1           TRUE
1           2           FALSE
1           3           TRUE
2           1           TRUE
SQLServer 2022/1/1 18:36:32 有417人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶