So here is the perfectly working query I need to run though short of the necessary condition:
所以这里是我需要运行的完美工作查询,但缺少必要条件:
INSERT INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified` FROM `pages`;
Unfortunately not all the databases are synced properly so some of the tables are populated and some are not.
遗憾的是,并非所有数据库都已正确同步,因此某些表格已填充,而某些表格则未填充。
How do I INSERT
data from table A to table B IF
table A is empty?
如何将表A中的数据插入表B如果表A为空?
A couple queries I've tried:
我试过几个查询:
IF (
SELECT count(id) FROM content='0',
INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified` FROM `pages`)
...as well as:
...以及:
IF (SELECT count(id) FROM content)=0
THEN (INSERT INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified` FROM `pages`);
1 个解决方案
#1
1
Try this:
INSERT INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified`
FROM `pages`
WHERE NOT EXISTS (SELECT 1 FROM content)
The SELECT
of the above INSERT
statement will return all pages
records unless there is at least on record in content
table.
除非内容表中至少有记录,否则上述INSERT语句的SELECT将返回所有页面记录。
Demo with empty table | Demo with not empty table
用空表演示|演示没有空表
#1
1
Try this:
INSERT INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified`
FROM `pages`
WHERE NOT EXISTS (SELECT 1 FROM content)
The SELECT
of the above INSERT
statement will return all pages
records unless there is at least on record in content
table.
除非内容表中至少有记录,否则上述INSERT语句的SELECT将返回所有页面记录。
Demo with empty table | Demo with not empty table
用空表演示|演示没有空表