获取具有多个条件的一个列值

时间:2017-12-20 10:16:04

标签: sql oracle oracle9i

我需要获取只有一个用户名的用户的域ID,该用户名应该以'a-%'或'A-%'开头,而所有其他用户名应该为system_id 349的“已禁用”,如下所示图像,

预期结果:

enter image description here

我使用的查询是:

SELECT * FROM
    (SELECT DISTINCT  p.domain_id
        FROM  ur_username u
            JOIN ur_username_person up ON u.username_id = up.username_id
            JOIN ur_person p ON up.person_id = p.person_id
            JOIN ur_system s ON u.system_id = s.system_id
          WHERE ( u.username LIKE 'A-%' OR username LIKE 'a-%') AND u.status = 'ACTIVE' AND u.system_id = 34)E
WHERE  E.domain_id IN  (SELECT  p.domain_id FROM ur_username u JOIN ur_username_person up ON u.username_id = up.username_id
    JOIN ur_person p ON up.person_id = p.person_id
  WHERE  u.system_id = 349 AND u.status = 'DISABLED')

例如,Pri_0911(domain_id)具有多个用户名,但他只有用户名以'a-%'开头且所有其他用户名都被禁用。因此我们需要选择与此条件匹配的域ID。

domain_id  | username    | status    |   system_id   |  
Pri_0911   | asdfg       | disabled  |      349      |
Pri_0911   | werdfdf     | disabled  |      349      |
Pri_0911   | zxgfg       | disabled  |      349      |
Pri_0911   | a-priya     | ACTIVE    |      349      |

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的最新要求,以下内容不会为您提供您所追求的内容吗?

SELECT DISTINCT p.domain_id,
                u.username
FROM   ur_username u
       JOIN ur_username_person up ON u.username_id = up.username_id
       JOIN ur_person p ON up.person_id = p.person_id
       JOIN ur_system s ON u.system_id = s.system_id
WHERE  u.username LIKE 'A-%'
OR     u.username LIKE 'a-%';