Sql 表中每个条件更新一条记录
我研究了这条线索,我想我会在那里找到我的答案,但不幸的是没有 如果我们的客户没有选择默认电子邮件地址,我需要更新该值 如果客户尚未收到默认电子邮件,则以下语句将更新该表的所有记录:Sql 表中每个条件更新一条记录,sql,oracle,Sql,Oracle,我研究了这条线索,我想我会在那里找到我的答案,但不幸的是没有 如果我们的客户没有选择默认电子邮件地址,我需要更新该值 如果客户尚未收到默认电子邮件,则以下语句将更新该表的所有记录: update si_contactemails set ISDEFAULT = 'Y' where entityid in (select customerid from si_customers where custstatus = 'A' and deleted = 0) and entityid in (s
update si_contactemails
set ISDEFAULT = 'Y'
where entityid in
(select customerid from si_customers where custstatus = 'A' and deleted = 0)
and entityid in (select entityid from si_contactemails group by entityid having MAX(ISDEFAULT) = 'N')
但是如果一个客户恰好在si_contactemails表中有多个条目,我只需要更新该客户的第一条记录,只能有一条默认记录
我尝试了上面提到的文章中的以下添加,但它只更新了所有条件都为真的第一条记录-如何更新所有条件为真的记录
update si_contactemails
set ISDEFAULT = 'Y'
where entityid in
(select customerid from si_customers where custstatus = 'A' and deleted = 0)
and entityid in (select entityid from si_contactemails group by entityid having MAX(ISDEFAULT) = 'N')
AND rowid = (SELECT min(rowid)
FROM si_contactemails
WHERE entityid in (select min(entityid) from si_contactemails group by entityid having MAX(ISDEFAULT) = 'N'))
欢迎任何意见:-)
非常感谢
Steph有一种方法:
UPDATE si_contactemails
SET isdefault = 'Y'
WHERE rowid IN
( SELECT MIN(rowid)
FROM si_contactemails
WHERE entityid IN
( SELECT customerid
FROM si_customers
WHERE custstatus = 'A'
AND deleted = 0
MINUS
SELECT entityid
FROM si_contactemails
WHERE isdefault = 'Y'
)
GROUP
BY entityid
)
;
它在si_contactemails
中为每个客户找到一个rowid
,(1)状态为A
且未被删除,(2)在si_contactemails
中没有默认记录。然后,它更新该组rowid
s中的每条记录
(免责声明:未经测试。)您能给我们举几个行的例子,您现在看到的情况,以及您希望看到的情况吗?