是否可以从表中获取具有引用其他两个表的值的数据(user_id和group_id之类的值)?
我在Oracle 11gR2中工作 问题出在表t3_access,列auth_id中。这是sql小提琴,以便更清楚地理解:http://sqlfiddle.com/#!2/b6dd7/5
结果集应如下所示:
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
我尝试了两种选择:
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%')
UNION
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);
任何帮助/查询/建议都会非常有用。
答案 0 :(得分:0)
此查询应提取您之后的信息:
select distinct t1.user_id, t1.name, t1.pwd_opts, t4.group_id,t2.conn_opts, t3.class_name, t3.access
from t1_users t1
join t2_connections t2
on t1.user_id = t2.user_id
join t4_groups t4
on t2.group_id = t4.group_id
join t3_access t3
on t4.group_id = t3.auth_id or t1.user_id = t3.auth_id
order by user_id;
基本上,它只是加入所有表,并在加入or
时在on
子句中使用t3_access
运算符。
<强> SQL Fiddle 强>
我没有深入研究这个问题,但我的直觉告诉我这个数据模型存在一些缺陷。 t3_access
和t1_users
以及t4_groups
之间似乎存在关联。表之间的关联取决于auth_id
列,它存储值而不是键。