Sql 在表列具有混合值的多个表上进行联接

Sql 在表列具有混合值的多个表上进行联接,sql,join,oracle11g,Sql,Join,Oracle11g,嗨,伙计们!在过去的几天里,我一直在网上搜索s的解决方案等等。这是我的第一个问题,所以请耐心点:) 在解释之前,如果太简单,我想道歉,但是我已经尝试了我能想到的一切,但我什么都没有。那么,让我们开始:) 问题是在t3_access.auth_id中有像user_id和group_id这样的值,因此我需要编写一个查询,列出t3_access.auth_id列中列出的所有用户以及t3_access.auth_id中组后面的用户 甚至可以在某些查询中区分与用户id或组id连接的用户吗?任何帮助都是非常

嗨,伙计们!在过去的几天里,我一直在网上搜索s的解决方案等等。这是我的第一个问题,所以请耐心点:)

在解释之前,如果太简单,我想道歉,但是我已经尝试了我能想到的一切,但我什么都没有。那么,让我们开始:)

问题是在t3_access.auth_id中有像user_id和group_id这样的值,因此我需要编写一个查询,列出t3_access.auth_id列中列出的所有用户以及t3_access.auth_id中组后面的用户

甚至可以在某些查询中区分与用户id或组id连接的用户吗?任何帮助都是非常受欢迎的

以下是SQLFIDLE链接:

我有四张表,它们的结构如下:

t1_users (user_id, name, pwd_opts)
t2_connections (user_id, group_id, conn_opts)
t3_access (auth_id, class_name, gr_name)
t4_groups (group_id, group_name)
以下是示例数据:

create table t1_users ("user_id" varchar2(10), "name" varchar2(10), "pwd_opts" varchar2(10));
create table t2_connections ("user_id" varchar2(10), "group_id" varchar2(10), "conn_opts" varchar2(10));
create table t3_access ("auth_id" varchar2(10), "class_name" varchar2(10), "gr_name" varchar2(10), "access" varchar2(10));

create table t4_groups ("GROUP_ID" varchar2(10), "group_name" varchar2(20));

insert into t1_users ("user_id", "name", "pwd_opts", "usr_opts") values ('Peter','Peter Pan','OK','RESTRICTED');
insert into t1_users ("user_id", "name", "pwd_opts", "usr_opts") values ('George','George Michael','OK','NORMAL');
insert into t1_users ("user_id", "name", "pwd_opts", "usr_opts") values ('Danny','Danny Boy','LOCK','SPECIAL');
insert into t1_users ("user_id", "name", "pwd_opts", "usr_opts") values ('John','John Wayne','OK','NORMAL');


insert into t2_connections (user_id, group_id, conn_opts) values('Peter','GROUP1','NORMAL');
insert into t2_connections (user_id, group_id, conn_opts) values('Peter','GROUP2','NORMAL');
insert into t2_connections (user_id, group_id, conn_opts) values('George','GROUP2','SPECIAL');
insert into t2_connections (user_id, group_id, conn_opts) values('Danny','GROUP2','NORMAL');
insert into t2_connections (user_id, group_id, conn_opts) values('John','GROUP3','NORMAL');


insert into t3_access(auth_id, class_name, gr_name, access) values('Peter','class1','PROFILE1','READ');
insert into t3_access(auth_id, class_name, gr_name, access) values('GROUP2','class1','PROFILE2','READ');
insert into t3_access(auth_id, class_name, gr_name, access) values('GROUP3','class3','PROFILE3','UPDATE');
insert into t3_access(auth_id, class_name, gr_name, access) values('George','class2','PROFILE2','EXECUTE');
insert into t3_access(auth_id, class_name, gr_name, access) values('John','class4','PROFILE4','NONE');

insert into t4_groups (group_id, "group_name") VALUES ('GROUP1', 'first group');
insert into t4_groups (group_id, "group_name") VALUES ('GROUP2', 'second group');
insert into t4_groups (group_id, "group_name") VALUES ('GROUP3', 'third group');
我尝试过的(我尝试过联合,在中,存在于两个选择之间),但没有得到预期的结果:

select t2_connections.*, t3_access.*
from t2_connections
join t3_access ON t2_connections.group_id = t3_access.auth_id 
where group_id IN (select auth_id from t3_access where auth_id like 'GROUP%');

select t2_connections.*, t3_access.*
from t3_access
join t2_connections ON t3_access.auth_id = t2_connections.user_id
where auth_id IN (select auth_id from t3_access where user_id like 'George' or auth_id like 'Danny' or auth_id like 'Peter' or auth_id like 'John' );
…我也尝试过:

select t1_users.user_id, t1_users.name, t1_users.pwd_opts, t1_users.usr_opts,
       t2_connections.group_id, t2_connections.conn_opts, t3_access.class_name, t3_access.gr_name,
       t3_access.access
from t2_connections
join t1_users on t2_connections.user_id = t1_users.user_id
join t4_groups on t2_connections.group_id = t4_groups.group_id
join t3_access on t2_connections.group_id = t3_access.auth_id
where exists (select * from t3_access where t3_access.auth_id = t2_connections.user_id);
union 
select t1_users.user_id, t1_users.name, t1_users.pwd_opts, t1_users.usr_opts,
       t2_connections.group_id, t2_connections.conn_opts, t3_access.class_name, t3_access.gr_name,
       t3_access.access
from t2_connections
join t1_users on t2_connections.user_id = t1_users.user_id
join t4_groups on t2_connections.group_id = t4_groups.group_id
join t3_access on t2_connections.group_id = t3_access.auth_id
where exists (select * from t3_access where t3_access.auth_id = t2_connections.group_id);
预期输出应如下所示:

user_id|name |pwd_|usr_opts    |group_id|conn_opt|cl_name |gr_name   |access
Peter | bla1 | ok | restricted | group1 | normal | class1 | profile1 | read
Peter | bla1 | ok | restricted | group2 | normal | class1 | profile2 | read
George| bla3 | ok | normal     | group2 | special| class2 | profile2 | execute
George| bla3 | ok | normal     | group2 | special| class1 | profile2 | read
Danny | bla4 |lock| special    | group2 | normal | class1 | profile2 | read
John  | bla5 | ok | normal     | group2 | normal | class4 | profile4 | none
John  | bla5 | ok | noraml     | group3 | normal | class3 | profile3 | update

您的示例设置被修改如下

create table t1_users (user_id varchar2(10), name varchar2(20), pwd_opts varchar2(10), usr_opts varchar2(15));
create table t2_connections (user_id varchar2(10), group_id varchar2(10), conn_opts varchar2(10));
create table t3_access (auth_id varchar2(10), class_name varchar2(10), gr_name varchar2(10), acces varchar2(10));
create table t4_groups (group_id varchar2(10), group_name varchar2(20));

insert into t1_users (user_id, name, pwd_opts, usr_opts) values ('Peter','Peter Pan','OK','RESTRICTED');
insert into t1_users (user_id, name, pwd_opts, usr_opts) values ('George','George Michael','OK','NORMAL');
insert into t1_users (user_id, name, pwd_opts, usr_opts) values ('Danny','Danny Boy','LOCK','SPECIAL');
insert into t1_users (user_id, name, pwd_opts, usr_opts) values ('John','John Wayne','OK','NORMAL');

insert into t2_connections (user_id, group_id, conn_opts) values('Peter','GROUP1','NORMAL');
insert into t2_connections (user_id, group_id, conn_opts) values('Peter','GROUP2','NORMAL');
insert into t2_connections (user_id, group_id, conn_opts) values('George','GROUP2','SPECIAL');
insert into t2_connections (user_id, group_id, conn_opts) values('Danny','GROUP2','NORMAL');
insert into t2_connections (user_id, group_id, conn_opts) values('John','GROUP3','NORMAL');

insert into t3_access(auth_id, class_name, gr_name, acces) values('Peter','class1','PROFILE1','READ');
insert into t3_access(auth_id, class_name, gr_name, acces) values('GROUP2','class1','PROFILE2','READ');
insert into t3_access(auth_id, class_name, gr_name, acces) values('GROUP3','class3','PROFILE3','UPDATE');
insert into t3_access(auth_id, class_name, gr_name, acces) values('George','class2','PROFILE2','EXECUTE');
insert into t3_access(auth_id, class_name, gr_name, acces) values('John','class4','PROFILE4','NONE');

insert into t4_groups (group_id, group_name) VALUES ('GROUP1', 'first group');
insert into t4_groups (group_id, group_name) VALUES ('GROUP2', 'second group');
insert into t4_groups (group_id, group_name) VALUES ('GROUP3', 'third group');

commit;
我最接近您需要的是:

with all_users$ as (
    select
        U.user_id, U.name, U.pwd_opts, U.usr_opts, C.group_id, C.conn_opts, A.class_name, A.gr_name, A.acces
    from t3_access A
        join t1_users U on U.user_id = A.auth_id
        join t2_connections C on C.user_id = A.auth_id
),
all_groups$ as (
    select
        U.user_id, U.name, U.pwd_opts, U.usr_opts, C.group_id, C.conn_opts, A.class_name, A.gr_name, A.acces
    from t3_access A
        join t2_connections C on C.group_id = A.auth_id
        join t1_users U on U.user_id = C.user_id
    where exists (
            select 1
            from t4_groups G
            where G.group_id = A.auth_id
        )
)
select *
from all_users$ AU
where not exists (
        select 1
        from all_groups$ Gx
        where Gx.user_id = AU.user_id
            and Gx.group_id = AU.group_id
            and Gx.class_name = AU.class_name
    )
--
union all
--
select *
from all_groups$ AG
;

如果你问一个问题,我们可能会帮助你。第一次发帖,我不习惯编辑。。。你的例子不起作用。有些列不存在,有些列数据超过列宽,有些列名与大小写不匹配。结果应该像我在文章的示例输出中指定的那样,问题是在您的查询中有一些空值,不应该有任何空值。我知道设计很糟糕…你不明白。我看到我的查询结果中有空值。我在问:为什么不应该有空值?用准确无误的英语描述你想要实现的目标。因为这是一个记录用户连接的系统。这样,用户就可以与其用户id或组id进行连接。我需要获取用户与其用户id进行连接的记录,如果用户与其组id进行连接,则查询应返回属于t3.auth\u id列中组的所有用户(组2和组3用户)。因此,查询应返回与其用户id连接的用户以及隐藏在其组id后面的用户。如果我不够清楚,请原谅。如果这种设计无法实现,那么是否可以修改设计以实现这种查询?没问题。对我来说,这是一次心理锻炼。:-)好的,我根据你的说明更新了我的答案。查询仍然没有返回所需的内容。你确定你知道你需要什么吗?也许这是不可能的,因为糟糕的设计。。。我需要获取连接到目标数据库的所有用户连接。无论他是用他的id连接还是用他的组id连接,因此结果应该列出所有的用户id。这是一个奇怪的任务,因为我没有访问数据模型的权限,但模型可以更改,但我不知道如何实现这一点。。。我感谢你的努力