3

我正在研究这个问题好几天。我有一个oracle数据库。问题必须在一个查询中解决。No Function, Pocedure, ... 我要进行选择。当他有结果时,发布它们。否则应该有“空结果”。

select case 
when count(*) = 0    
then 'no Entry'   
else MAX(Member)--all Members should be here 
END as Member 
from tableMember 
where Membergroup = 'testgroup'; 

问题是 Oracle 想要一个由 else 提供的 Agregat 函数。因此,如果结果不是“无条目”,我只会得到一个值。我需要所有的价值观。

欢迎所有能帮助我的人,让我快乐。

4

5 回答 5

5

不知道你想达到什么目标,也许这个

 select member from tablemember where Membergroup = 'testgroup'
union 
 select  'no Entry' 
 from dual 
 where NOT EXISTS ( select member from tablemember where membergroup = 'testgroup')
;
于 2012-08-17T10:33:52.933 回答
4

不需要两个聚合查询,你只需要检查是否max(member)为空。我会这样做是为了弄清楚发生了什么。

select case when max_member is null then 'no entry' else max_member end as member
  from ( select max(member) as max_member
           from tablemember
          where membergroup = 'testgroup'
                )

但是,如果您想返回所有成员,您可以执行以下操作:

select member
  from tablemember
 where membergroup = 'testgroup'
 union all
select 'no entry'
  from dual
 where not exists ( select 1 from tablemember where membergroup = 'testgroup')
于 2012-08-17T10:26:29.457 回答
1

如果您RIGHT JOIN使用空集的查询进行查询,您将始终获得一行,并且如果您的查询返回数据,则将获得所有行。这比 aUNIONUNION ALL使用 a更便宜(更快),NOT EXISTS因为它不需要对数据进行多次扫描。

SELECT nvl(a.member,b.member) member
   FROM (SELECT member FROM tablemember WHERE membergroup='????') a
   RIGHT JOIN (SELECT 'no Entry' member FROM dual) b ON 1=1;

测试环境:

DROP TABLE tablemember;
CREATE TABLE tablemember AS
   (
      SELECT TO_CHAR(level) member
         , DECODE(mod(level, 5), 0, 'testgroup', 'othergroup') membergroup
      FROM dual CONNECT BY level <= 50
   );
于 2012-08-17T12:50:50.320 回答
0

您可以使用一些聚合函数和 NVL 来实现您的目标:

SELECT MIN('VALUE 1') AS p1, MIN('VALUE 2') AS p2 FROM DUAL WHERE 1=0

此查询的结果是:NULL,NULL

接下来,用所需的字符串替换空值:

SELECT 
    NVL(MIN('1'), 'empty  value 1') AS p1, 
    NVL(MIN('STRING VALUE'), 'empty value 2')  AS p2,
    NVL(MIN((select 'subquery result' from dual)), 'empty subquery result') as p3
FROM 
    DUAL 
WHERE 
    1=0

但是,您不能在字段中混合数字和字符串。

于 2013-03-21T05:25:35.247 回答
-2

试试这个:

DECLARE C INTEGER;

SELECT COUNT(*) INTO C FROM tableMember WHERE Membergroup = 'testgroup';

IF C > 0

THEN

    SELECT * FROM tableMember;

ELSE

    SELECT 'No results!' FROM tableMember;

END IF;
于 2012-08-17T11:23:12.353 回答