2

我正在尝试为表中的每个字段分别插入两个数据,如下所示:

INSERT INTO `wp_postmeta` (post_id, meta_key, meta_value)
SELECT post_id, 'custom', custom
SELECT post_id, '_custom', 'field_514e91e73640d'
FROM `wp_temp`
WHERE custom IS NOT NULL

但是,这不起作用。无论如何在MySQL的一个查询中实现这一点?谢谢。

4

3 回答 3

6
INSERT INTO `wp_postmeta` (post_id, meta_key, meta_value)
SELECT post_id, 'custom', custom FROM `wp_temp` WHERE custom IS NOT NULL
UNION ALL
SELECT post_id, '_custom', 'field_514e91e73640d' FROM `wp_temp` WHERE custom IS NOT NULL
于 2013-10-04T13:55:15.093 回答
1

您将需要一个单独的FROMandWHERE子句SELECT,但可以使用 UNION 或 UNION ALL。

INSERT INTO `wp_postmeta` (post_id, meta_key, meta_value)
SELECT post_id, 'custom', custom
FROM `wp_temp`
WHERE custom IS NOT NULL
UNION 
SELECT post_id, '_custom', 'field_514e91e73640d'
FROM `wp_temp`
WHERE custom IS NOT NULL
于 2013-10-04T13:56:33.240 回答
0

是的UNION ALL

INSERT INTO `wp_postmeta` (post_id, meta_key, meta_value)
SELECT post_id, '_custom', 'field_514e91e73640d'
FROM `wp_temp`
WHERE custom IS NOT NULL

UNION ALL

SELECT post_id, 'custom', custom
FROM `wp_temp`
WHERE custom IS NOT NULL
于 2013-10-04T13:55:43.363 回答