有没有简单的方法在重复键后更新所有值?例如:
INSERT INTO published_books
SELECT * FROM books
WHERE book_id = book_id
ON DUPLICATE KEY UPDATE ?everything?
该表有大约50列,每次更新都会很痛苦。有什么想法吗?
答案 0 :(得分:4)
您可以将REPLACE INTO
用于此目的:
REPLACE INTO published_books SELECT * from books;
答案 1 :(得分:0)
如果你可以生成查询,你可以使用这样的东西(PHP):
$fields = array(
'field_1',
'field_2',
'field_3'
);
$query = '
INSERT INTO table (
' . implode(', ', $fields) . '
) SELECT
-- ...
ON DUPLICATE KEY UPDATE ';
foreach ($fields as $i => $field) {
$query .= ($i > 0 ? ', ' : '') . $field . '=VALUES(' . $field . ')';
}
答案 2 :(得分:-3)
INSERT INTO published_books(col1, col2)
VALUES (’xxx’, ‘yyy’) ON DUPLICATE KEY UPDATE col1 = VALUES(col1)