如何将特定列单元格映射到同一 table 中的其他特定列单元格?
How to map specific column cells to other specific column cells in same table?
我正在尝试查看是否可以用同一列中其他位置的一组单元格替换列中一组单元格的内容 table。
示例:
Book
-------
id payload
1 a
2 b
3 c
4 d
5 e
我想要一个查询,使其成为以下内容
Book
-------
id payload
1 c
2 e
3 c
4 d
5 e
假设我想要 3,5 的有效负载替换 1,2 的内容。
这是我的尝试:
update
(
select id, payload
from Books
where id in (1,2)
) as normal_book_1
join
(
select id, payload
from Books
where id in (3,5)
) as normal_book_2
set normal_book_1.payload = normal_book_2.payload;
您可以执行两个 UPDATE
语句,如:
update books
set payload = (select payload from books where id = 3)
where id = 1
update books
set payload = (select payload from books where id = 5)
where id = 2;
您可以使用 ON
子句中的列映射进行自连接:
UPDATE Book b1
INNER JOIN Book b2 ON (b1.id, b2.id) IN ((1, 3), (2, 5))
SET b1.payload = b2.payload;
参见demo。
我正在尝试查看是否可以用同一列中其他位置的一组单元格替换列中一组单元格的内容 table。
示例:
Book
-------
id payload
1 a
2 b
3 c
4 d
5 e
我想要一个查询,使其成为以下内容
Book
-------
id payload
1 c
2 e
3 c
4 d
5 e
假设我想要 3,5 的有效负载替换 1,2 的内容。
这是我的尝试:
update
(
select id, payload
from Books
where id in (1,2)
) as normal_book_1
join
(
select id, payload
from Books
where id in (3,5)
) as normal_book_2
set normal_book_1.payload = normal_book_2.payload;
您可以执行两个 UPDATE
语句,如:
update books
set payload = (select payload from books where id = 3)
where id = 1
update books
set payload = (select payload from books where id = 5)
where id = 2;
您可以使用 ON
子句中的列映射进行自连接:
UPDATE Book b1
INNER JOIN Book b2 ON (b1.id, b2.id) IN ((1, 3), (2, 5))
SET b1.payload = b2.payload;
参见demo。