Sql 如果Oracle未返回任何行,请选择另一条语句
我在SQL方面很弱。我有一个3列的小表和基本的select语句Sql 如果Oracle未返回任何行,请选择另一条语句,sql,oracle,Sql,Oracle,我在SQL方面很弱。我有一个3列的小表和基本的select语句 select intl, loc FROM test_table where p.country = 'UAE' 但在某些情况下,没有返回行,在这些情况下,我希望从同一个表中选择另一条语句 我找到了下面的查询,但无法使其正常工作 select intl, loc FROM test_table where p.country = 'UAE' union all select 'intl','
select intl, loc FROM test_table where p.country = 'UAE'
但在某些情况下,没有返回行,在这些情况下,我希望从同一个表中选择另一条语句
我找到了下面的查询,但无法使其正常工作
select intl, loc FROM test_table
where p.country = 'UAE'
union all
select 'intl','Loc' from dual
where not exists ( select intl, loc FROM test_table where p.country = 'Other' )
我知道这很简单,但我被困了好几个小时。
Thanx您似乎想要的查询是:
select intl, loc
from test_table
where p.country = 'UAE'
union all
select 'intl', 'Loc' from dual
where not exists ( select intl, loc from test_table where p.country = 'UAE' );
不存在的国家/地区应与原始查询中的国家/地区相同。您似乎想要的查询是:
select intl, loc
from test_table
where p.country = 'UAE'
union all
select 'intl', 'Loc' from dual
where not exists ( select intl, loc from test_table where p.country = 'UAE' );
select intl, loc
from test_table
where p.country = 'UAE'
union all
select intl, loc from test_table
where not exists ( select intl, loc from test_table where p.country = 'UAE' );
不存在的国家/地区应与原始查询中的国家/地区相同
select intl, loc
from test_table
where p.country = 'UAE'
union all
select intl, loc from test_table
where not exists ( select intl, loc from test_table where p.country = 'UAE' );
这样比较好
这样更好。如果我正确理解您的需求,可能会有一点不同的方法:
SELECT intl, loc from test_table WHERE p.country =
(CASE WHEN (SELECT intl FROM test_table WHERE p.country='UAE' AND ROWNUM=1) IS NOT NULL THEN 'UAE'
ELSE 'Other' END);
看起来您希望在中使用一个值,其中p.country=
或另一个值。因此,这个CASE
决定了使用第一个p.country
值进行选择是否会返回一些结果(我们必须使用ROWNUM=1
限制结果,以获得准确的一个结果,否则该CASE将不起作用-它需要一个值。如果是(不是空的
),那么我们在的WHERE
中使用该值,否则我们在的ELSE
后面使用一个不同的值。如果我正确理解了您的需求,可能会有一点不同的方法:
SELECT intl, loc from test_table WHERE p.country =
(CASE WHEN (SELECT intl FROM test_table WHERE p.country='UAE' AND ROWNUM=1) IS NOT NULL THEN 'UAE'
ELSE 'Other' END);
看起来您希望在中使用一个值,其中p.country=
或另一个值。因此,这个CASE
决定了使用第一个p.country
值进行选择是否会返回一些结果(我们必须使用ROWNUM=1
限制结果,以获得准确的一个结果,否则该CASE将不起作用-它需要一个值。如果是(不是空的
),那么我们在的WHERE
中使用该值,否则我们将使用在ELSE
后面指定的其他值,谢谢。这就是我需要的谢谢。这就是我想要的need@ShB . . . 这很模糊。@ShB。这很模糊。