oracle 9中非常简单的SQL查询

时间:2009-11-27 10:17:00

标签: sql oracle

我有两张桌子:

emp (empno,ename)
address(empno,address)

\ temp可以有0个或更多地址。

我想按如下方式打印结果:

  • 当emp有0地址时:eno,ename,“no address”
  • 当emp有1个或更多地址时:enomename,“address found”

我正在使用oracle 9db

3 个答案:

答案 0 :(得分:3)

select empno, ename, 
    case when (select count(1) from address where empno=emp.empno)>0 
         then 'adress found'
         else 'no address'
    end
  from emp

答案 1 :(得分:0)

试试这个

输入:

地址表

empno   address
1   address1 for name1
1   address2 for name1
2   address1 for name2
2   address2 for name2
2   address3 for name2

Emloyee表

empno   ename
1   name1
2   name2
3   name3
4   name4
5   name5

<强>查询

select distinct(e.empno),e.ename,
    case when
    a.address IS null then 'No Address' else 'Address found' end as Status
    from @emp e
    left join @address a
    on e.empno  = a.empno 

输出:

empno   ename      Status
1        name1  Address found
2        name2  Address found
3        name3  No Address
4        name4  No Address
5        name5  No Address

答案 2 :(得分:0)

从中选择e.empno,e.ename,'Address Found'作为状态 emp e,地址a 其中e.empno = a.empno
并且a.address不为空 UNION ALL
从中选择e.empno,e.ename,'No Address'作为状态 emp e,地址a 其中e.empno = a.empno
和a.address为空