简体   繁体   中英

How to find records having specific repetitions mysql

I have a large table with 800k records. 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?

if alone mysql is not enough for it then anything in a php script is also welcomed.

thank you for your time and consideration.

For deleting the whole duplicate book_id s you can use this query:


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:


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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