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

2017-12-28 11:46:07 +08:00
 poupoo

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 怎么实现?

1329 次点击
所在节点    问与答
6 条回复
poupoo
2017-12-28 11:47:46 +08:00
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
2017-12-28 14:32:15 +08:00
按你说,4 符合不?
poupoo
2017-12-28 15:20:53 +08:00
@lansediao 符合啊
poupoo
2017-12-28 15:22:05 +08:00
今天大神都去哪了?儿白
sun1991
2017-12-28 15:42:32 +08:00
给你个伪代码实现:
```
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
2017-12-28 15:44:42 +08:00
@lansediao
已解决 用 oracle LISTAGG 函数聚合到一块再做判断,谢谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/418246

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX