是否可以使用与外键连接的表中的数据更新行?

发布于 2024-11-24 04:15:01 字数 572 浏览 3 评论 0原文

我的数据库中有一个表 [Book],其中包含 [AuthorId][Description] 列。另一个表[Author]通过外键[Book].[AuthorId] -> 与[Book]相关。 [作者].[ID]

我想使用文本 “来自 #Author# 的好书” 更新 [Book].[Description] 列,其中 #Author# 是取自 [Author].[Name] 的作者姓名。

像这样的事情:

UPDATE [Book] SET [Description] = 'Nice book from ' + [Author].[Name]

但问题是我不知道是否有办法从 UPDATE 语句中加入作者和书籍,以便每个更新的行都知道它的作者名字。

这可以在单个 SQL 查询中实现吗?

I have a table [Book] in a database that has a [AuthorId] and [Description] columns. The other table [Author] is related to the [Book] via foreign key [Book].[AuthorId] -> [Author].[ID].

I'd like to update the [Book].[Description] column with the text "Nice book from #Author#", where #Author# is the name of the author taken from the [Author].[Name].

Something like this:

UPDATE [Book] SET [Description] = 'Nice book from ' + [Author].[Name]

but the problem is that I don't know if there is a way to join the author and the book from within the UPDATE statement, so that each updated row will know it's author's name.

Is this possible in a single SQL query?

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

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

发布评论

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

评论(2

落墨 2024-12-01 04:15:01
update Book 
  set Description = 'Nice book from '+Author.Name
from Author
where Book.AuthorID = Author.AuthorID
update Book 
  set Description = 'Nice book from '+Author.Name
from Author
where Book.AuthorID = Author.AuthorID
つ可否回来 2024-12-01 04:15:01
UPDATE [Book] SET [Description] = 'Nice book from ' + [Book].[Name]
FROM [Book], [Author] 
Where [Book].AuthorId = [Author].Id
UPDATE [Book] SET [Description] = 'Nice book from ' + [Book].[Name]
FROM [Book], [Author] 
Where [Book].AuthorId = [Author].Id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文