1

我正在使用下面的插入查询为所有 wordpress 帖子添加自定义文件,但是出现 Sql 语法错误我不知道我在哪里做错了

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'CustomField'
AS meta_key 'MyValue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'CustomField')
'' AND post_type = 'post'; 
4

1 回答 1

2

您错过了用于分隔列的逗号和用于包装字符串的单引号。

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 
       'CustomField' AS meta_key, 
       'MyValue' AS meta_value
FROM   wp_posts 
WHERE  ID NOT IN
       (SELECT post_id 
        FROM wp_postmeta 
        WHERE meta_key = 'CustomField')
       AND post_type = 'post'; 

使用的另一种方法NOT IN是连接两个表,

INSERT  INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT  a.ID AS post_id, 
        'CustomField' AS meta_key, 
        'MyValue' AS meta_value
FROM    wp_posts a
        LEFT JOIN wp_postmeta b
            ON  a.ID = b.post_id AND 
                b.meta_key = 'CustomField'
WHERE   a.post_type = 'post' AND
        b.post_id IS NULL
于 2013-03-21T07:52:32.593 回答