为 table 多次指定了列 xxx

The column xxx was specified multiple times for table

我正在尝试使 db2 简单查询适应 SQL 服务器。此查询在 db2

上运行良好
select * 
from pb_console.users u 
join ( 
     select * from pb_console.users_user_role j join  
     pb_console.users_roles r on j.role_id = r.role_id) as jj 
on jj.user_id = u.user_id

在 sql 服务器上失败并出现错误:

The column 'ROLE_ID' was specified multiple times for 'jj'

我已尝试从联接的左侧 table 删除角色 _id 为:

select * from pb_console.users u join ( 
    select user_id, role_rif from 
    pb_console.users_user_role j join (select role_id, role_name from 
pb_console.users_roles) r 
on 
j.role_id = r.role_id) as jj on jj.user_id = u.user_id

但结果是。

The column 'role_id' was specified multiple times for 'jj'.

我也曾尝试为第一个 role_id 使用不同的别名,但没有成功。

我该如何解决这个问题?

你的代码中出现错误:Incorrect syntax near the keyword 'from',因为你没有在 select 语句中指定任何列,请在下面的查询语句中指定所需的列

select from pb_console.users_roles

应该是

select role_id from pb_console.users_roles

错误:

The column 'role_id' was specified multiple times for 'jj'.

尝试在所有 select 语句中使用 alias.columnname

select * from pb_console.users u join ( 
    select j.user_id, j.role_id from 
    pb_console.users_user_role j join (select role_id, role_name from 
pb_console.users_roles) r 
on 
j.role_id = r.role_id) as jj on jj.user_id = u.user_id

出现错误是因为 ColumnNameROLE_ID 在 table pb_console.users_user_role & pb_console.users_roles 中都存在,因此请尝试指定仅需要如下的列

SELECT * 
FROM pb_console.users u 
join ( 
     SELECT J.RoleID,J.User_ID FROM pb_console.users_user_role j 
     JOIN pb_console.users_roles r ON j.role_id = r.role_id) AS jj 
on jj.user_id = u.user_id
select * from pb_console.users

不应使用语法。

处理这个问题的静态方法: 在 SSMS 编辑器中,右键单击 pb_console.users table 以获取 "select *" 详细语法...并覆盖之前由 Joby 或 Sandip 告知的别名。

动态方式: 创建一个存储过程来检索 table 的当前结构,通过 "EXECUTE" 构建一个 sql 语句 运行...更难的方法...

请不要使用 from * 代替 * 中的列名,列名可能会相互冲突。

在我的例子中作为底部代码:

select a.*,b.*,a.x1 from a
join b on b.fkId=a.pkid

a.* 和 a.x1 相互冲突,因为 a.* 包含 a.x1 列,而 return table 有 2 个名称为 x1 的列。