简体   繁体   English

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

[英]Is it possible to Update the rows with data from a table connected with the foreign key?

I have a table [Book] in a database that has a [AuthorId] and [Description] columns.我在具有[AuthorId][Description]列的数据库中有一个表[Book] The other table [Author] is related to the [Book] via foreign key [Book].[AuthorId] -> [Author].[ID] .另一个表[Author]通过外键[Book].[AuthorId] -> [Author].[ID][Book]相关。

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] .我想用文本"Nice book from #Author#"更新[Book].[Description]列,其中#Author#是取自[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.但问题是我不知道是否有办法从UPDATE语句中加入作者和书,以便每个更新的行都知道它的作者姓名。

Is this possible in a single SQL query?这在单个 SQL 查询中是否可行?

update Book 
  set Description = 'Nice book from '+Author.Name
from Author
where Book.AuthorID = Author.AuthorID
UPDATE [Book] SET [Description] = 'Nice book from ' + [Book].[Name]
FROM [Book], [Author] 
Where [Book].AuthorId = [Author].Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM