Oracle SQL查询联接同一表 [英] Oracle SQL query joining same table

查看:228
本文介绍了Oracle SQL查询联接同一表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表:

                 items
id            old_new     object 

1               o         pen
2               n         house
3               o         dog
4               o         cat
5               n         carrot

我想要选择返回:

id    new_object     old_object

1        null          pen
2        house        null
3        null         dog
4        null         cat  
5        carrot       null 

我需要在同一张表上使用外部联接吗?

Do I need to use an outer join on the same table?

推荐答案

无需连接:

select id, 
       case when old_new = 'n' then object end as new_object,
       case when old_new = 'o' then object end as old_object
from the_table
order by id;

这篇关于Oracle SQL查询联接同一表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆