日期:2014-05-17  浏览次数:20954 次

同一张表,根据查询条件的不同,对结果集合并,并标记状态
现有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