【问题标题】:Create an Update statement using a Having Count clause on sqlplus在 sqlplus 上使用 Have Count 子句创建更新语句
【发布时间】:2014-03-27 15:28:32
【问题描述】:

我想从使用 HAVING COUNT 的 select 语句创建更新查询

这是选择查询:

select hlse_ref, HLSE_TERM_START_DATE, HLSE_STATUS_CODE
from headleas a, lernhist b
where a.hlse_ref=b.lerh_leas_hlse_ref
and a.HLSE_TERM_START_DATE is not null
group by a.hlse_ref, a.HLSE_STATUS_CODE, a.HLSE_TERM_START_DATE
having count(b.LERH_START_DATE)=1

我想将 hlse_term_start_date 设置为 null 条件满足我的 select 语句中的条件

update headleas
set hlse_term_start_date=null
where( *my select statement*)

但这似乎不起作用。我看过其他帖子,但他们对这个案子没有太大帮助。我正在使用 SQL Plus。有什么想法吗?

【问题讨论】:

  • 您是否记住,您需要通过列链接这两个查询,例如... WHERE ID IN (select statement with one column) ... ?

标签: sql oracle count sqlplus having


【解决方案1】:

我不知道您要构建的逻辑,但这可能会有所帮助

使用像

这样的存在子句
update headless outer
set hlse_term_start_date=null
where exists ( select 1 from headleas a, lernhist b
where a.hlse_ref=b.lerh_leas_hlse_ref
and a.HLSE_TERM_START_DATE is not null
and outer.hlse_ref = a.hlse_ref
group by a.hlse_ref, a.HLSE_STATUS_CODE, a.HLSE_TERM_START_DATE
having count(b.LERH_START_DATE)=1 
)

比较 where 子句中的子查询计数,如

update headless outer
set hlse_term_start_date=null
where 1= (
select count(b.LERH_START_DATE)
from headleas a, lernhist b
where a.hlse_ref=b.lerh_leas_hlse_ref
and a.HLSE_TERM_START_DATE is not null
and a.hlse_ref = outer.hlse_ref
group by a.hlse_ref, a.HLSE_STATUS_CODE, a.HLSE_TERM_START_DATE
)

【讨论】:

  • 谢谢,您的第一个更新查询对我来说就像一个魅力!我所需要的只是 where exists 子句。
【解决方案2】:

您的 where 条件应该是什么?您给出的示例语法不正确。

update table_b b
set b.col1 = null
where (select col2 from table_a a) --incorrect

这不是 where 条件;它不会评估为真或假。您的意思是使用“存在的地方”吗?还是其他一些条件?如果您使用“where exists”,则必须使用某个键值(可能是 HEADLEAS 的主键)将您的 select 语句与更新后的表关联起来。

【讨论】:

  • 是的,对不起。我现在看到我必须使用 where exists 子句,这就是我的更新查询不起作用的原因。我已经加入了我的 PK,即 hlse_ref 和 lerh_leas_hlse_ref。
猜你喜欢
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多