select 没有行时获取案例结果字段

Get case result field when select don't have rows

让我们更好地解释一下:

只有当某些条件为真时,我才需要更新 table。

当条件为真时,更新发生,我取回更新的字段和使用返回键更新的行的 ID。

问题是条件不满足,所以更新没有发生,我的结果集是空的...

您可以通过这些脚本进行模拟:

create table wn_liberacao (

  id serial not null primary key,
  data_inicial date,   
  data_final date,
  consulta_limite integer not null,   
  consulta_utilizada integer not null

);

insert into 
  wn_liberacao ( data_inicial, data_final, consulta_limite, consulta_utilizada )
        values ( '2015-01-01', '2020-12-31', 10, 0 );

所以,当您 运行 这个查询时:

with liberado as (

update wn_liberacao
   set consulta_utilizada = 1 + consulta_utilizada
  from ( select id from wn_liberacao where ( data_inicial <= current_date ) and ( data_final >= current_date ) and ( consulta_limite > consulta_utilizada )
--         and 1=2 
       ) liberacao
 where liberacao.id = wn_liberacao.id

returning wn_liberacao.id id_liberacao, wn_liberacao.consulta_limite, wn_liberacao.consulta_utilizada

) 

select 
  case liberado.id_liberacao
    when null then 'NOT' --0
    else 'YES' --1
  end liberou, liberado.*
  from liberado

这个查询的第 10 次 运行s,我收到 YES....但第 11 次,结果为 null,没有数据行....

我需要第11次后,'liberou'字段的结果是'NOT',其他字段可以为null...

我该怎么做???

select true as liberou, liberado.*
from liberado
union all
select false, null, null, null

order by liberou desc
limit 1

false 订单在 true 之前,所以如果有 A 返回行,将返回 'true' 行,否则将返回 false 行。

这个查询:

with liberado as (

update wn_liberacao wn
   set consulta_utilizada = 1 + consulta_utilizada
  from ( select id from wn_liberacao where ( data_inicial <= current_date ) and ( data_final >= current_date ) and ( consulta_limite > consulta_utilizada )
--         and 1=2 
       ) liberacao
 where liberacao.id = wn.id

returning wn.id id_liberacao, wn.consulta_limite, wn.consulta_utilizada

) 

select 1 as liberou, liberado.*
from liberado
union all
select 0, null, null, null

order by liberou desc
limit 1

解决了我的问题...感谢Clodoaldo Neto