我在使用 mySQL 语句时遇到了一些麻烦,我确信该语句并不复杂,但我不太熟悉 mySQL,所以现在我陷入了困境。我所说的数据库是 wordpress,所以希望很多人都能理解其结构,但我会尽力解释我需要什么以及我尝试过什么......
我的问题是这样的:
我有一些数据需要从一个摘录复制到另一个摘录...... 基本上,我已经在大量附件中输入了标题,但我需要将该标题复制到“下载”post_types 上的 post_excerpt。它们在数据库中通过一些名为“_download_attachment_id”的元数据链接 因此,我需要将“下载”post_excerpt 设置为附件 post_excerpt,其中“下载”ID 等于 postmeta 表中的 meta_value(附件 ID)。
我意识到附件“标题”存储在 wp_posts 中的“post_excerpt”列中,因此我对如何将一个 post_excerpt 更新为另一个 post_excerpts 的值感到困惑。
为了尝试使其更容易,我使用此 SQL 创建了一个 VIEW
CREATE VIEW `attachment_excerpt` AS
SELECT a.ID aid, a.post_excerpt as excerpt, b.post_id pid
FROM rpz_posts a, rpz_postmeta b
WHERE a.ID = b.meta_value
AND b.meta_key = '_download_attachment_id'
这给了我一个 View ,其中包含我需要的所有内容,但名称不同。现在,通过该 View 设置,我可以使用以下 SQL 语句来获取我需要的所有内容...
SELECT rpz_posts.ID, attachment_excerpt.excerpt
FROM rpz_posts, attachment_excerpt
WHERE rpz_posts.ID = attachment_excerpt.pid
AND rpz_posts.post_excerpt = '';
这给了我需要更新的帖子的ID,以及我需要更新的摘录文本。我无法工作的是更新...我尝试了这个,但没有成功:
UPDATE rpz_posts SET rpz_posts.post_excerpt = attachment_excerpt.excerpt
WHERE rpz_posts.ID = attachment_excerpt.pid
AND rpz_posts.post_excerpt = '';
我得到的错误是: #1054 - “where 子句”中的未知列“attachment_excerpt.pid”
我意识到这是一篇很长的文章,所以我希望我没有让它变得过于复杂。如果有人能指出我正确的方向,我将永远感激不已!
最佳答案
您的更新应如下所示:
UPDATE rpz_posts,attachment_excerpt
SET rpz_posts.post_excerpt = attachment_excerpt.excerpt
WHERE rpz_posts.ID = attachment_excerpt.pid
AND rpz_posts.post_excerpt = '';
这是因为您必须指明涉及的两个表才能执行匹配。
MySQL docs更新中。
关于MySQL 从同名的另一列更新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28422229/