mardi 5 mai 2015

How to find duplicate content and make the last entry the master id?

I'm having some issues with mysql and I hope you'll be able to help me. :)

This code is for simplifying search results on a library website. One book can be published many time but the content of the book remains the same. This is why only one book needs to appear when we search for "Ze Book" or one of its ISBNs.

Here is the table I have:

id | isbn    | bookname | disabled | alias
1  | A-B-C-D | Ze Book  | 0        |
2  | E-F-G-H | Ze Book  | 0        |
3  | I-J-K-L | Ze Book  | 0        |
4  | M-N-O-P | Bookeee  | 0        |
5  | Q-R-S-T | Bookeee  | 0        |

And this is what I would like to do for table:

id | isbn    | bookname | disabled | alias
1  | A-B-C-D | Ze Book  | 1        |
2  | E-F-G-H | Ze Book  | 1        |
3  | I-J-K-L | Ze Book  | 0        |A-B-C-D, E-F-G-H
4  | M-N-O-P | Bookeee  | 1        |
5  | Q-R-S-T | Bookeee  | 0        |M-N-O-P

I do not know where to begin exactly and especially if it could be done directly with mysql.

For the moment, I've managed to identify the duplicate book names:

SELECT bookname, COUNT(*) AS number
FROM table
GROUP BY bookname
HAVING COUNT(*) >1
ORDER BY number DESC

Any ideas what to do next?

Thank's a lot in advance, as you may have noticed, I'm still learning how to code. :)

Aucun commentaire:

Enregistrer un commentaire