SQL Server 2008 在连接表中使用 Join 和Where 子句更新查询

发布于 2024-09-19 21:03:22 字数 576 浏览 5 评论 0原文

不知道为什么这不起作用:

UPDATE 
    ust
SET  
    ust.isUnsubscribedFromSystemEmails = 1
FROM         
    UserSetting AS ust 
INNER JOIN
    [User] ON ust.userID = [User].userID 
AND 
    [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses)

用简单的英语来说,我试图将 isUn​​subscribed 字段设置为取消订阅,其中 UserSetting 表中的 userID等于用户表中的userID,并且用户表中的emailAddress 不在另一个表的电子邮件列表中。我可以使用几乎相同的语法在 isUnsubbed 列上运行选择,并且它工作正常吗?谢谢!

PS 我在这里查看了其他类似的问题,语法看起来相同,但显然我遗漏了一些东西。

Not sure why this is not working:

UPDATE 
    ust
SET  
    ust.isUnsubscribedFromSystemEmails = 1
FROM         
    UserSetting AS ust 
INNER JOIN
    [User] ON ust.userID = [User].userID 
AND 
    [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses)

In plain English, I am trying to set the isUnsubscribed field to unsubscribed where the userID in the UserSetting table equals the userID in the user table and where the emailAddress in the user table is not in a list of emails from another table. I can run a select on the isUnsubbed column using pretty much the same syntax and it works fine? thanks!

P.S. I've looked at other similar questions here and the syntax appears the same but obviously I'm missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放低过去 2024-09-26 21:03:22

是的,你忽略了一些事情。

set 语句不能引用集合左侧的别名。

尝试:

UPDATE  
    ust 
SET   
    isUnsubscribedFromSystemEmails = 1 
--select *
FROM          
    UserSetting AS ust  
INNER JOIN 
    [User] ON ust.userID = [User].userID  
WHERE [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses) 

我添加了注释掉的选择,以便您可以检查是否获得了您想要的结果集。

Yep you've overlooked something.

The set statement cannot reference the alias on the left side of the set.

Try:

UPDATE  
    ust 
SET   
    isUnsubscribedFromSystemEmails = 1 
--select *
FROM          
    UserSetting AS ust  
INNER JOIN 
    [User] ON ust.userID = [User].userID  
WHERE [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses) 

I added the commented out select so you can check to see that you aregetting results set you wanted.

双手揣兜 2024-09-26 21:03:22

尽管 UPDATE...FROM 语法在某些情况下至关重要,但我更喜欢尽可能使用子查询。这能满足您的需要吗?

UPDATE UserSetting
SET isUnsubscribedFromSystemEmails = 1
WHERE userID in (SELECT userID from [User]
                WHERE emailAddress in (SELECT emailAddress FROM BadEmailAddresses))

Although the UPDATE...FROM syntax is essential in some circumstances, I prefer to use subqueries whenever possible. Does this do what you need?

UPDATE UserSetting
SET isUnsubscribedFromSystemEmails = 1
WHERE userID in (SELECT userID from [User]
                WHERE emailAddress in (SELECT emailAddress FROM BadEmailAddresses))
因为看清所以看轻 2024-09-26 21:03:22

试试这个:

UPDATE UserSetting ust SET usr.isUnsubscribedFromSystemEmails = 1
WHERE ust.emailAdress IN (select emailAddress from bademailAddresses);

Try this :

UPDATE UserSetting ust SET usr.isUnsubscribedFromSystemEmails = 1
WHERE ust.emailAdress IN (select emailAddress from bademailAddresses);
喜你已久 2024-09-26 21:03:22

尝试:

UPDATE  
    UserSetting
SET   
    isUnsubscribedFromSystemEmails = 1 
FROM          
    UserSetting 
INNER JOIN 
    [User] ON UserSetting.userID = [User].userID  
AND  
    [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses)

Try:

UPDATE  
    UserSetting
SET   
    isUnsubscribedFromSystemEmails = 1 
FROM          
    UserSetting 
INNER JOIN 
    [User] ON UserSetting.userID = [User].userID  
AND  
    [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses)
↘人皮目录ツ 2024-09-26 21:03:22

注意:仅供记录(假设其他一切都正常工作),您还可以在 BadEmailAddresses 表上执行内部联接。

如果您遇到任何性能问题,那么您可能需要为两个表中的 emailAddress 列建立索引。

Note: Just for the record (assuming you get everything else to work), you could also do an inner join on the BadEmailAddresses table.

If you have any performance problems, then you might want to index the emailAddress column in both tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文