这是我的表的当前状态:
mysql> select * from page; +----+----------+----------------+------+---------+ | id | title | body | page | visible | +----+----------+----------------+------+---------+ | 1 | my title | my body | NULL | 1 | | 2 | my title | my body edited | 1 | 0 | +----+----------+----------------+------+---------+ 2 rows in set (0.00 sec)
我希望第1行包含第2行的值.基本上,我想这样做:
UPDATE page SET page.* = (SELECT * FROM page WHERE id = 2) WHERE id = 1;
这样的事情可能吗?
插入页面(id,title,body,page,visible)选择2,标题,正文,页面,可见
通过使用ON DUPLICATE KEY UPDATE,可以在没有DELETE的情况下完成(仅限MySQL):
INSERT INTO page (id, title, body, page, visible) SELECT 1, title, body, page, visible FROM page WHERE id=2 ON DUPLICATE KEY UPDATE title= VALUES(title), page= VALUES(page), visible= VALUES(visible);
但是,也可以通过自联接以符合ANSI的方式完成(可能更好):
UPDATE page AS page1 JOIN page AS page2 ON page1.id=1 AND page2.id=2 SET page1.title=page2.title, page1.body= page2.body, page1.page= page2.page, page1.visible=page2.visible