当前位置: 代码迷 >> Oracle开发 >> 同一张表,依据查询条件的不同,对结果集合并,并标记状态
  详细解决方案

同一张表,依据查询条件的不同,对结果集合并,并标记状态

热度:19   发布时间:2016-04-24 07:09:28.0
同一张表,根据查询条件的不同,对结果集合并,并标记状态
现有1张表:
id   userId    ringId
1     1000       111
2     1000       222
3     1000       333

4     1001       111
5     1001       444
6     1001       555

我要查询出userId为1001的结果集,同时跟userId为1000的结果集用ringId进行匹配,如果ringId相同,则标记为1,不相同标记为0,查询到的结果如下:

id    userId    ringId   state
4      1001      111       1
5      1001      444       0
6      1001      555       0

求sql。
sql oracle

------解决方案--------------------
select id,
        userid,
        ringid,
        sign((select count(*)
               from table b
              where userid = 1000
                and a.ringid = b.ringid)) state
   from table a
  where a.userid = 1001
  相关解决方案