如果另一个 table 中不存在,如何从中删除

How to delete from one table if it do not exist in the another

我有一个 MySQL 数据库,其中有两个 table。一个 table“url”和一个名为“stats”的。

在 table“url”中,“id”列与 table“stats”中的“page_title”列具有完全相同的值".

我想做的是检查 table“stats.page_title”与“url.id”中的每个条目。如果在“url.id”中找不到来自“stats.page_title”的值,则应删除来自“stats.page_title”的条目。

我怎么能这样?

NOT EXISTS:

delete from stats
where not exists (select 1 from url where url.id = stats.page_title)

LEFT JOIN:

delete s
from stats s
left join url u on u.id = s.page_title
where u.id is null

条件where u.id is null将只删除不匹配的行。