简体   繁体   English

如何在'WHERE'子句sql中回退到不同的值?

[英]How to fallback to a different value in 'WHERE' clause sql?

I have a case where i have to fetch records for column field1='value1' if there are no values for 'value1' then i should fetch the record for 'default'. 我有一个案例,我必须获取列field1 ='value1'的记录,如果没有'value1'的值,那么我应该获取'默认'的记录。

For the above scenario I have used two queries: 对于上面的场景,我使用了两个查询:

Select * from table_name where field1="value1"

If the above query does not give back any record I fire the following query: 如果上面的查询没有回复任何记录,我会触发以下查询:

Select * from table_name where field1="default"

Now I want to do the above stated in one query. 现在我想在一个查询中执行上述操作。 Can someone please help me with the same. 有人可以帮我一样。 I believe the answer lies somewhere in using CASE WHEN clause. 我相信答案在于使用CASE WHEN子句。

Also the above queries should work for oracle, postgres as well as mysql. 此外,上述查询应该适用于oracle,postgres以及mysql。

Core ANSI SQL answer, expected to run on all different platforms: 核心ANSI SQL答案,预计将在所有不同平台上运行:

select * from table_name
where field1 = 'value1'
  or (field1 = 'default'
      and NOT EXISTS (select 1 from table_name where field1 = 'value1'))

Optimal solution using coalesce() : 使用coalesce()最佳解决方案:

Select * from table_name where field1 = coalesce (
    (select field1  from table_name where field1='value1' limit 1)
    , 'default'
);

Notice the limit 1 in the subquery: In this case it is mandatory to ensure that subselect doesn't return more than one row. 请注意子查询中的limit 1 :在这种情况下,必须确保subselect不返回多行。 But even using the case when exists (...) approach it is a good practice to add it because, otherwise, database engine would be forced to scan all rows matching the subquery. 但即使使用case when exists (...)方法的case when exists (...) ,添加它也是一种好习惯,否则,数据库引擎将被强制扫描与子查询匹配的所有行。

Of course, most modern databases are smart enough to silently optimize it. 当然,大多数现代数据库都足够智能,可以默默地优化它。 But some old ones could not. 但有些旧的不能。 And anyway, it can be cases where they cant. 无论如何,它可能是他们无法解决的情况。

That is, for example, in PostgreSQL, if subquery uses non (or non properly declared as) stable functions, the planner would be forced to perform a full scan to produce consistent results if that function has any side effect. 也就是说,例如,在PostgreSQL中,如果子查询使用非(或非正确声明的)稳定函数,则如果该函数有任何副作用,则计划程序将被强制执行完整扫描以产生一致的结果。

Use CASE and Exists like below 使用CASE和Exists如下所示

Select * from table_name where field1=
    case when exists(select 1  from table_name  where field1='value1')
    then 'value1' else 'default 'end

For Mysql: 对于Mysql:

SET @a = Select Count(*) from table_name where field1="value1"
IF @a > 0 Then
Select 
    * 
from 
    table_name 
where 
   field1="value1"
ELSE
Select 
    * 
from 
    table_name 
where 
   field1="default"
END IF

Okay , i tried out this it seems to be working . 好吧,我试过这个似乎工作。

SELECT * FROM table_name WHERE  
CASE
  WHEN EXISTS(SELECT * FROM table_name WHERE field1='value1') THEN field1= 'value1'
  ELSE field1='default'
END

You could try this... 你可以尝试这个......

select * table_name where field1="default"
and not exists (select * from table_name where field1="value1")
union all
select * from table_name where field1="value1"

You can use case statement with count check. 您可以使用带有count检查的case陈述。

select *
  from table_name
 where coulmn_1 = (case
         when (select count(1) from dc_date_card where coulmn_1 = value_1) > 0 then
 value_1 
 else
  Default 
  end)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM