用于从其他两个表的信息更新表字段的SQL查询

时间:2021-04-09 00:10:37

I have three tables: category, old, and new.

我有三个表:category,old和new。

Goal: to update book_id from old to new

目标:将book_id从旧更新为新

tables category has a column: book_id

tables类有一列:book_id

tables old and new both have columns: id and isbn

旧表和新表都有列:id和isbn

What I want to achieve:

我想要实现的目标:

  • take the original book_id from category
  • 从类别中取出原始book_id

  • match original book_id with id from old
  • 将原始book_id与旧的id匹配

  • take the resulting record's isbn and match it with the isbn in new
  • 获取结果记录的isbn,并将其与isbn in new匹配

  • take the resulting record's id and update book_id
  • 获取结果记录的id并更新book_id

Running MySQL.

2 个解决方案

#1


2  

MySQL syntax is a little different that SQL Server or Oracle...

MySQL语法与SQL Server或Oracle略有不同......

update category cat
inner join old on cat.book_id = old.id
inner join new on old.isbn = new.isbn
set cat.book_id = new.id

#2


0  

If you update the category this should do it:

如果您更新类别,则应执行以下操作:

update cat 
set book_id = new.id
from category cat
join old on cat.book_id = old.id
join new on old.isbn = new.isbn

#1


2  

MySQL syntax is a little different that SQL Server or Oracle...

MySQL语法与SQL Server或Oracle略有不同......

update category cat
inner join old on cat.book_id = old.id
inner join new on old.isbn = new.isbn
set cat.book_id = new.id

#2


0  

If you update the category this should do it:

如果您更新类别,则应执行以下操作:

update cat 
set book_id = new.id
from category cat
join old on cat.book_id = old.id
join new on old.isbn = new.isbn