oraclesql与3个表的连接

oknrviil  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(467)

lk\ U状态表

id  desc
MD  Maryland
FL  Florida
VA  Virginia
NY  NewYork
NJ  NewJersy
CA  California

用户表

user_ID Name    Active  State
01      AAA     1       MA  
02      BBB     1       MD
03      CCC     1       CA
04      DDD     1       NY
05      EEE     1       NJ
06      FFF     1       FL
07      GGG     1       FL

用户组表

id  group
01  10
02  20
03  20
04  30
05  20
06  10
07  20

我想加入表,这将给我的lk\ U状态表的状态输出。条件是只显示用户属于role=20的那些状态
输出应为

id  desc
MD  Maryland
FL  Florida
NJ  NewJersy
CA  California

我尝试加入,但它给了我多行,每个用户和组一行。请帮助我使用按要求显示的oracle sql。

3wabscal

3wabscal1#

试试这个:

SELECT DISTINCT s.*
FROM lk_states s, users u, users_groups g
WHERE s.id = u.state
  AND u.user_id = g.id
  AND g.group = 20
p4tfgftt

p4tfgftt2#

你可以用 exists 以及相关子查询:

select s.*
from lk_states s
where exists (
    select 1
    from users u
    inner join user_groups ug on ug.id = u.user_id
    where u.state = s.id and ug.group = 20
)
wztqucjr

wztqucjr3#

你试过这个吗。将关键字保留为列名并不理想

Select s.* 
from lk_states s
JOIN users u ON u.state = s.id
JOIN users_groups ug ON ug.id = u.user_id
WHERE ug.u_group  = 20

相关问题