查询从两个表中列出具有相同配置文件的用户

时间:2017-12-05 13:24:24

标签: sql oracle

Table 1 : dbt_m_user (user_id, user_name, email_id, . . . . . )

Table 2 : dbt_m_user_profile (user_id, profile_id, . . . . .)

我有具有特定个人资料(角色)的用户的电子邮件ID。 现在我需要具有相同访问配置文件的用户名

我可以从下面得到结果,但它看起来很原始,看起来并不优雅,考虑到只有2个表。

  select user_name from dbt_m_user where user_id in (
  select user_id from dbt_m_user_profile where profile_id in (
  select profile_id from dbt_m_user_profile where user_id in ( 
  select user_id from  dbt_m_user where Email_Id = 'snehal.masne@db.com' )))

这可以通过使用特殊条款/连接来即兴创作吗?

3 个答案:

答案 0 :(得分:1)

以下查询应该有效:

select t1.user_name
from dbt_m_user_profile t1
inner join dbt_m_user t2
on t2.user_id = t1.user_id
where t2.profile_id in (select t1.profile_id
                       from dbt_m_user_profile t1
                       inner join (select * from dbt_m_user 
                                   where t1.email_id = 'snehal.masne@db.com'
                                  ) t2
                       on t2.user_id = t1.user_id                           
                       )
;

答案 1 :(得分:0)

这个怎么样?我还没有测试过它

   SELECT u.username
     FROM dbt_m_user u 
     JOIN dbt_m_user_profile p
      ON  (u.user_id = p.user_id)
     JOIN dbt_m_user_profile up
      ON  (p.profile_id = up.profile_id)
    JOIN  dbt_m_user ou
      ON  (up.user_id = ou.user_id)
   WHERE ou.email_id = 'snehal.masne@db.com'; 

答案 2 :(得分:0)

尝试以下查询

SELECT    U.USER_NAME 
FROM      DBT_M_USER_PROFILE P, DBT_M_USER U 
WHERE     P.USER_ID = U.USER_ID 
AND       P.PROFILE_ID IN  (
              SELECT P.PROFILE_ID
              FROM  DBT_M_USER_PROFILE P, DBT_M_USER U
              WHERE U.EMAIL_ID = 'snehal.masne@db.com'
              AND  P.USER_ID = U.USER_ID  )