我有一个SQL查询,它使用UPDATE语句计算我需要更新其信息的确切填充。但它包含一些复杂的连接,我不确定如何在UPDATE语句中重新创建该语句。有什么帮助吗?
我的查询在这里:
select distinct c.id
from implementation.tt_ngma_exclude_emails nee
join customer c on nee.util_account_id = c.util_internal_id
join customer_notification_contact cnc on cnc.customer_id = c.id
left join customer_notification_contact_audit cnca on cnca.customer_id = c.id
where cnc.changed_on = '2015-11-15 12:30:02';
此处的目标是更新customer_notification_contact
表中的特定字段,而不是我选择的实现表。我想将cnc表中的email
字段设置为NULL,cnc.customer_id = c.id
这是我的尝试,但它似乎不起作用:
UPDATE customer_notification_contact cnc
(select distinct cnc.customer_id opower_customer_id
from implementation.tt_ngma_exclude_emails nee
join customer c on nee.util_account_id = c.util_internal_id
join customer_notification_contact cnc on cnc.customer_id = c.id
where cnc.changed_on = '2015-11-15 12:30:02'
) T2
SET cnc.email = NULL
WHERE cnc.customer_id = T2.opower_customer_id;
答案 0 :(得分:1)
请尝试将T1.SOME_COLUMN
(最后一行)替换为您要更新的实际列名。我添加了GROUP BY CNC.CUSTOMER_ID,因为当您更新多行时,您应该知道该计数属于哪个客户。除非你试图用我假设你不想做的相同计数来更新所有行。
UPDATE customer_notification_contact T1,
(
select count(distinct c.id) AS MY_COUNT,CNC.CUSTOMER_ID
from implementation.tt_ngma_exclude_emails nee
join customer c on nee.util_account_id = c.util_internal_id
join customer_notification_contact cnc on cnc.customer_id = c.id
left join customer_notification_contact_audit cnca on cnca.customer_id = c.id
where cnc.changed_on = '2015-11-15 12:30:02'
GROUP BY CNC.CUSTOMER_ID
)T2
SET T1.SOME_COLUMN = T2.MY_COUNT
WHERE T1.CUSTOMER_ID = T2.CUSTOMER_ID
更新在看到更新后的问题后,这应该是查询。
UPDATE customer_notification_contact T1,
(
select distinct c.id
from implementation.tt_ngma_exclude_emails nee
join customer c on nee.util_account_id = c.util_internal_id
join customer_notification_contact cnc on cnc.customer_id = c.id
left join customer_notification_contact_audit cnca on cnca.customer_id = c.id
where cnc.changed_on = '2015-11-15 12:30:02'
)T2
SET T1.EMAIL = NULL
WHERE T1.CUSTOMER_ID = T2.id