MARIADB:根据来自另一个 table 的数据插入和更新 table
MARIADB : Insert and Update table based on data from another table
/* 这是我用来合并的代码,但我收到错误并且无法更新
错误:SQL 错误 [1064][42000] 您的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在第 1 行 'Merge temp2 as t' 附近使用的正确语法
*/
BEGIN
MERGE temp2 as t
using temp1 as s ON (t.slno = s.slno)
-- Insert values when data no present
WHEN NOT MATCHED THEN INSERT VALUES
(s.slno,s.name,s.address);
-- Update when values present
WHEN MATCHED then UPDATE SET
t.slno = s.slno,
t.name = s.name,
t.address = s.address;
END
你可能会用这个:
INSERT INTO temp2 (slno, name, address) SELECT slno, name, address FROM temp1
ON DUPLICATE KEY UPDATE
slno = VALUES(slno), name = VALUES(name), address = VALUES(address)
/* 这是我用来合并的代码,但我收到错误并且无法更新 错误:SQL 错误 [1064][42000] 您的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在第 1 行 'Merge temp2 as t' 附近使用的正确语法 */
BEGIN
MERGE temp2 as t
using temp1 as s ON (t.slno = s.slno)
-- Insert values when data no present
WHEN NOT MATCHED THEN INSERT VALUES
(s.slno,s.name,s.address);
-- Update when values present
WHEN MATCHED then UPDATE SET
t.slno = s.slno,
t.name = s.name,
t.address = s.address;
END
你可能会用这个:
INSERT INTO temp2 (slno, name, address) SELECT slno, name, address FROM temp1
ON DUPLICATE KEY UPDATE
slno = VALUES(slno), name = VALUES(name), address = VALUES(address)