SQL IF表A是表B中的空副本列

时间:2016-05-20 06:21:24

标签: mysql

所以这里是我需要运行的完美工作查询,但缺少必要的条件:

INSERT INTO content (`id`,`id_pages`,`content`, `date`) 
SELECT `id`, `id`, `content`, `date_modified` FROM `pages`;

不幸的是,并非所有数据库都已正确同步,因此部分表格已填充,而某些表格则未填充。

我如何从表A到表B的INSERT数据IF表A是空的?

我尝试了几个问题:

IF (
SELECT count(id) FROM content='0',
INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified` FROM `pages`)

......以及:

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 个答案:

答案 0 :(得分:1)

试试这个:

INSERT INTO content (`id`,`id_pages`,`content`, `date`)
SELECT `id`, `id`, `content`, `date_modified` 
FROM `pages`
WHERE NOT EXISTS (SELECT 1 FROM content)

上述SELECT语句的INSERT将返回所有pages条记录,除非至少记录在content表中。< / p>

Demo with empty table | Demo with not empty table