gpt4 book ai didi

mysql - 在 wordpress 数据库中的 INSERT 查询中获取 SQL 语法错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:14:57 26 4
gpt4 key购买 nike

我正在使用下面的插入查询为所有 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';

最佳答案

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

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

关于mysql - 在 wordpress 数据库中的 INSERT 查询中获取 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15542047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com