简体   繁体   English

MySQL查询会删除A表中未在B表上找到ID的所有行?

[英]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_hashpage 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. 不幸的是,由于维护的原因,我丢失了一些数据,结果是表behaviourunique_hashunique hash数量大于audienceunique 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.

相关问题 PHP MySQL:从表A获取行,其中表B的Z列中不存在表A的Z列的值 - PHP MySQL: Getting rows from table A where values of column Z of Table A not present in column Z of table B 使用表A中的ID对表B中的行进行计数 - Counting rows from table B using id from table A php mysql PHP MYSQL 显示表 a 中的所有值,但只显示表 b 中的匹配值,其中表 b 是单独的循环 - PHP MYSQL display all values from table a but only matching values from table b where table b is separate loop mysql显示表中添加的所有行 - mysql display all rows from table that where added 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table mysql查询-仅从另一个表中选择id = id,而该表中的字段=值 - mysql query - only select where id = id from another table and a field in that table = a value 将表B中的多行链接到MySQL中的表A中的一行 - Linking multiple rows from table B to one row in table A in MySQL MySQL - 使用表 a 从表 b 获取信息,其中 = - MySQL - get information from table b using table a as where = MySQL / PHP-选择表1中的列等于表2中的列的所有行,并创建一个foreach循环 - MySQL/PHP - Select all the rows where column from table 1 is equal to column from table 2 and creating a foreach loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM