我正在尝试为表中的每个字段分别插入两个数据,如下所示:
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的一个查询中实现这一点?谢谢。
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
您将需要一个单独的FROM
andWHERE
子句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
是的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