[英]MySQL query that deletes all rows from A table where the id is not found on B table?
In my mySQL database, under the table audience
there is a column called unique_hash
where it has unique identifiers for each of my visitors. 在我的MySQL数据库,在桌子底下
audience
有一列名为unique_hash
那里有我的每个访问者的唯一标识符。
In the table behaviour
there is a column unique_hash
and page
. 在表
behaviour
有一列unique_hash
和page
。 A unique_hash
can have many rows, contrary to the audience
table. 与
audience
表相反, unique_hash
可以有很多行。 However, the unique_hash
found on behaviour
are from the visitors that already saved them into the audience
table. 但是,在
behaviour
上发现的unique_hash
来自已将其保存到audience
表的访问者。
Unfortunately for some maintenace reasons, I lost some of my data, and the result is that the distinct number of unique_hash
in table behaviour
are more than the unique hash
of audience
, the time that this should be equal. 不幸的是,由于维护的原因,我丢失了一些数据,结果是表
behaviour
中unique_hash
的unique hash
数量大于audience
的unique hash
,即相等的时间。
What is the mysql query that will delete all the rows in the behaviour
table where the unique_hash
are not found on the audience
table? 什么是MySQL查询,将删除所有的行
behaviour
表所在的unique_hash
没有在发现audience
表?
You can use NOT IN
like the following: 您可以使用
NOT IN
,如下所示:
DELETE FROM behaviour
WHERE NOT unique_hash IN (
SELECT DISTINCT unique_hash FROM audience
)
A second solution would be to use NOT EXISTS
: 第二种解决方案是使用
NOT EXISTS
:
DELETE FROM behaviour
WHERE NOT EXISTS (
SELECT * FROM audience
WHERE audience.unique_hash = behaviour.unique_hash
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.