poupoo
V2EX  ›  问与答

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

  •  
  •   poupoo · Dec 28, 2017 · 2206 views
    This topic created in 3238 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
    poupoo
    OP
    Dec 28, 2017
    1
    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
    lansediao
    Dec 28, 2017
    2
    按你说,4 符合不?
    poupoo
    poupoo
    OP
    Dec 28, 2017
    3
    @lansediao 符合啊
    poupoo
    poupoo
    OP
    Dec 28, 2017
    4
    今天大神都去哪了?儿白
    sun1991
    sun1991
    Dec 28, 2017❤️1
    5
    给你个伪代码实现:
    ```
    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
    poupoo
    OP
    Dec 28, 2017
    6
    @lansediao
    已解决 用 oracle LISTAGG 函数聚合到一块再做判断,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   983 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 19:18 · PVG 03:18 · LAX 11:18 · JFK 14:18
    ♥ Do have faith in what you're doing.