首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

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

2013-01-11 
同一张表,根据查询条件的不同,对结果集合并,并标记状态现有1张表:iduserIdringId110001112100022231000333

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

热点排行