简体   繁体   English

如何查找具有特定重复的记录 mysql

[英]How to find records having specific repetitions mysql

I have a large table with 800k records.我有一张包含 800k 条记录的大表。 The structure of the table is as follows:表的结构如下:

Table: chapters
Columns: Id chapter author  pages   book_id

Some of the records have repetitions according to the following pattern:根据以下模式,一些记录有重复:

id  chapter author  pages   book_id
64478   Veronica Vasterling & Silvia Stoller            1665112051456
64479   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen         1665112051456
64480   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües            1665112051456
64481   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling            1665112051456
64482   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling Helen A. Fielding          1665112051456
64483   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling Helen A. Fielding Gabrielle Hiltmann           1665112051456
64484   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling Helen A. Fielding Gabrielle Hiltmann Silvia Stoller            1665112051456
64485   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling Helen A. Fielding Gabrielle Hiltmann Silvia Stoller Kelly Oliver           1665112051456
64486   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling Helen A. Fielding Gabrielle Hiltmann Silvia Stoller Kelly Oliver Sara Heinämaa         1665112051456
64487   Veronica Vasterling & Silvia Stoller Debra B. Bergoffen Christina Schües Veronica Vasterling Helen A. Fielding Gabrielle Hiltmann Silvia Stoller Kelly Oliver Sara Heinämaa Annemie Halsema         1665112051456

is there any way to find and delete the whole book_id of such records?有什么办法可以找到并删除此类记录的整个 book_id 吗?

if alone mysql is not enough for it then anything in a php script is also welcomed.如果单独使用 mysql 还不够,那么也欢迎使用 php 脚本中的任何内容。

thank you for your time and consideration.感谢您的时间和考虑。

For deleting the whole duplicate book_id s you can use this query:要删除整个重复的 book_id ,您可以使用此查询:


delete from chapters where book_id in 
  (select book_id from chapters group by book_id having count(*) > 1);

If you want to keep the last book_id and delete the rest you can use this code:如果你想保留最后一个 book_id 并删除 rest 你可以使用这个代码:


delete from chapters where 
   id not in (select max(id) from chapters group by book_id );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM