【问题标题】:Selecting multiple rows from the same table with common column?从具有公共列的同一个表中选择多行?
【发布时间】:2021-05-04 04:36:26
【问题描述】:

Oracle DB 中有如下假设表 User_Mail_Info

| User_Id | Mail_DL | DLId     |.....
--------------------------------
|  User1  |  DLA    |   1      |
|  User1  |  DLB    |   2      |
|  User2  |  DLA    |   1      |
|  User2  |  DLB    |   2      |
|  User2  |  DLC    |   3      |......
|  User3  |  DLA    |   1      |
|  User3  |  DLC    |   2      |
|  User3  |  DLB    |   3      |......

主键是 User_Id,Mail_DL,DLId。表格中还有其他列。

给定(Mail_DL 和 DL Id)的列表,如何检索 UserId ?

注意:Mail_DL 和 DL_Id 没有直接关系。这只是假设数据

例如。 输入是

  • (DLA, 1), (DLB,2) - 输出必须是 User1
  • (DLA, 1), (DLB,2), (DLC,3) - 输出必须是 User2
  • (DLA, 1), (DLC,2), (DLB, 3) - 输出必须是 User3

提前感谢您的帮助。

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您可以使用聚合和过滤。第一个例子:

    select user_id
    from t
    group by user_id
    having sum(case when mail_id = 'DLA' and dlid = 1 then 1 else 0 end) > 0 and
           sum(case when mail_id = 'DLB' and dlid = 2 then 1 else 0 end) > 0 and
           count(*) = 2;
       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多