poupoo
V2EX  ›  问与答

SQL 求助,各位大神求带飞!

  •  
  •   poupoo · Dec 28, 2017 · 1826 views
    This topic created in 3057 days ago, the information mentioned may be changed or developed.

    x | y

    1 a 2 a+b 3 a 3 b 2 c 1 b+d 4 a+b 1 c+e 5 a+b+c+d 2 d 4 c+e 5 f 3 e 6 a+b+c 3 d

    x y 对应的是两列,我想取 y 包含( a 和 b 和 e 这里比如 3 是满足的它在不同的行里同时拥有 abe,6 是不满足的因为它只有 abc )所有 x ,请问 sql 怎么实现?

    6 replies    2017-12-28 15:44:42 +08:00
    poupoo
        1
    poupoo  
    OP
       Dec 28, 2017
    1:a
    2:a+b
    3 :a
    3:b
    2:c
    1:b+d
    4 :a+b
    1 :c+e
    5:a+b+c+d
    2:d
    4:c+e
    5:f
    3:e
    6:a+b+c
    3:d

    x y 对应的是两列,我想取 y 包含( a 和 b 和 e 这里比如 3 是满足的它在不同的行里同时拥有 abe,6 是不满足的因为它只有 abc )所有 x ,请问 sql 怎么实现?
    lansediao
        2
    lansediao  
       Dec 28, 2017
    按你说,4 符合不?
    poupoo
        3
    poupoo  
    OP
       Dec 28, 2017
    @lansediao 符合啊
    poupoo
        4
    poupoo  
    OP
       Dec 28, 2017
    今天大神都去哪了?儿白
    sun1991
        5
    sun1991  
       Dec 28, 2017   ❤️ 1
    给你个伪代码实现:
    ```
    select x from
    (
    select x,
    case when y contains 'a' then 1 else 0 end as col1,
    case when y contains 'b' then 1 else 0 end as col2,
    case when y contains 'e' then 1 else 0 end as col3,
    from tbl
    ) tbl2 where col1 = 1 and col2 = 1 and col3 = 1;
    ```
    poupoo
        6
    poupoo  
    OP
       Dec 28, 2017
    @lansediao
    已解决 用 oracle LISTAGG 函数聚合到一块再做判断,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3203 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 14:26 · PVG 22:26 · LAX 07:26 · JFK 10:26
    ♥ Do have faith in what you're doing.