i am using below insert query adding custom filed for all wordpress post, but getting Sql Syntax error i dont know where i m doing wrong
我正在使用下面的插入查询添加自定义提交所有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';
1 个解决方案
#1
2
you missed comma for separation of columns and single quote to wrap string.
你错过了逗号分隔列和单引号来换行。
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';
An alternative of using NOT IN
is by joining two tables,
使用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
#1
2
you missed comma for separation of columns and single quote to wrap string.
你错过了逗号分隔列和单引号来换行。
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';
An alternative of using NOT IN
is by joining two tables,
使用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