2012-03-21 47 views
-3

我有这个(工作)选择statment:从选择要更新的Oracle SQL

select * from memberships where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null 
and name_id in (select name_id from name where history_yn = 'N') 

,但现在我想改变它,所以这将是一个更新语句,:

update name 
set history_yn = 'Y' 
IN (select * from memberships where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null) 

,但我收到ora-00933错误。你能提供这方面的建议吗?

+1

什么是memberships'和'name''之间的关系。或者,表格的结构和它们之间的关系是什么? – GolezTrol 2012-03-21 11:03:02

回答

2

事情是这样的:

update name set history_yn = 'Y' 
where name_id IN (select name_id 
        from memberships 
        where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null) 
and history_yn = 'N' 
+0

嗨,当我使用我的选择我收到50个结果。但是当我使用更新时,我删除了200多行...你知道为什么吗? – 2012-03-21 12:54:36

+0

也许你应该在末尾加上'and history_yn ='N''。看到我更新的答案。 – 2012-03-21 13:02:25

+0

工程很棒,Thx很多。还没有投票:( – 2012-03-21 13:36:32

1

你需要有之前WHERE子句。查询可改写为

update name set history_yn = 'Y' 
WHERE name_id 
IN (select name_id from memberships where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null) 

更有效的方法可能是使用exists代替IN子句如下

update name n set history_yn = 'Y' 
WHERE EXISTS 
(select name_id from memberships where MEMBERSHIP_TYPE = 'ZZZ' 
    and inactive_date is null 
    and name_id = n.name_id) 
+0

嗨,当我使用我的选择我收到50个结果但是当我使用更新时,我删除了200多行......你有什么想法为什么? – 2012-03-21 12:57:06