如何在另一个表中缺少值后清理表?

发布于 2024-11-16 00:46:14 字数 540 浏览 3 评论 0原文

我尝试将 bbPress 1.0 安装迁移到 WordPress 上的新 bbPress 2.0 插件,但没有成功,这使我的 wp_posts 表变得一团糟。

我通过删除对 post_type 列携带 topicreply 值的所有帖子解决了这个问题,这很好。

但现在我注意到 wp_postmeta 似乎受到许多引用我已删除的帖子的条目的影响。两个表都有 post_id 列。

问题是:我使用哪个 SQL 命令来删除 wp_postmeta 上引用不再位于 wp_posts 上的行的值?我知道这是我应该使用的某种连接,但我不知道如何查找不存在的内容并将其删除。

PS:WordPress 表格不保持引用完整性是怎么回事?我非常确定在删除 wp_posts 上的相关内容时可以自动从 wp_postmeta 中删除某些内容。那好吧…

I was unsuccessful trying to migrate a bbPress 1.0 instalation to the new bbPress 2.0 plugin on my WordPress, which made quite a mess on my wp_posts table.

I solved that by deleting every posts who was carrying the values topic and reply to the post_type column, and that was good.

But now I noticed that the wp_postmeta seems affected by lots of entries refering to posts that I've deleted. Both tables have the post_id column.

The question is: which SQL command I use to delete the values on wp_postmeta referencing lines who aren't anymore on wp_posts? I know it's some kind of join that I should use but I don't see how it would be to look for something who is not there and delete it.

PS: And what's up with WordPress tables not keeping referential integrity? I'm pretty sure it's possible to delete something automatically from wp_postmeta when deleting something related on wp_posts. Oh well…

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-11-23 00:46:14

手动删除表 a 中与表 b 中不匹配的行的一般方法是:

DELETE FROM table_a WHERE some_id NOT IN (SELECT some_id FROM table_b);

这可能不是执行批量删除的最有效方法(联接可能会更快),但我通常更喜欢在以下情况下使用子选择这样,它更容易理解,因此降低了 FUBAR 概率(您不想意外删除 table_b 等中的行),

因此在您的情况下,您可能可以这样做:

SELECT * FROM wp_postmeta WHERE post_id NOT IN (SELECT post_id FROM wp_posts);

如果您确定,这些是您想要的行要删除,请将 SELECT * 替换为 删除:

DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT post_id FROM wp_posts);

a general way to manually delete rows from table a that have no match in table b is:

DELETE FROM table_a WHERE some_id NOT IN (SELECT some_id FROM table_b);

this may not be the most efficient way to do a mass delete (joins could probably do it faster), but I usually prefer a subselect in cases like this, it is easier to understand and therefore reduces the FUBAR probability (you don't want to accidentally delete rows in table_b etc)

so in your case you could probably do something like this:

SELECT * FROM wp_postmeta WHERE post_id NOT IN (SELECT post_id FROM wp_posts);

if you are sure, these are the rows you want to delete, replace SELECT * with DELETE:

DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT post_id FROM wp_posts);
定格我的天空 2024-11-23 00:46:14

我知道这是我应该使用的某种联接,但我不知道如何查找不存在的内容并将其删除。

是的,您想使用外连接。

PS:WordPress 表格不保持引用完整性是怎么回事?

许多较新的应用程序现在使用实体框架,并且不显式强制执行引用完整性,仅强制执行关系。例如,Atlassian JIRA 不强制执行引用完整性。

I know it's some kind of join that I should use but I don't see how it would be to look for something who is not there and delete it.

Yes, you want to use outer joins.

PS: And what's up with WordPress tables not keeping referential integrity?

A lot of newer applications now use entity frameworks and don't explicitly enforce referential integrity, just relationships. Atlassian JIRA, for example, doesn't enforce referential integrity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文