以下代码用于将行插入到 mySQL 数据库上的 table_main 中,其中 table_A 包含 Primary_isbn13 列中的唯一值
INSERT INTO `table_main`
SELECT * FROM `table_A` A
WHERE NOT EXISTS (SELECT 1 FROM `table_main` X
WHERE A.`primary_isbn13` = X.`primary_isbn13`)
我有几个表要合并到 table_main 中,其中存在唯一的primary_isbn13。 而不是继续运行相同的sql语句多次,用table_B替换table_A,然后用table_C替换table_C等,有没有办法选择多个表-table_A,table_B和table_C并将它们合并到存在唯一primary_isbn13值的table_main中?
使用
UNION
:
INSERT INTO table_main
SELECT * FROM table_A A
WHERE NOT EXISTS (SELECT 1 FROM table_main X WHERE A.primary_isbn13 = X.primary_isbn13)
UNION
SELECT * FROM table_B B
WHERE NOT EXISTS (SELECT 1 FROM table_main X WHERE B.primary_isbn13 = X.primary_isbn13)
UNION
SELECT * FROM table_C C
WHERE NOT EXISTS (SELECT 1 FROM table_main X WHERE C.primary_isbn13 = X.primary_isbn13)