Search code examples
sqlmysql

How to fix MySQL providing duplicates that do not exist?


I have been recently messing with MySQL as I'm using it in a current project, I have a few thousand records in a table but there's one which stands out to me, I have a SELECT statement which collects a bunch of column names and uses them for the final query to send.
However when I run the query, it gives me duplicates as seen here: https://i.sstatic.net/zxr6v.png

The strange thing is that the ID is set as the key, so there's no right for MySQL to produce duplicates, and even if I go into the table and check manually, no duplicates exist.

This query used to work without a hitch on this exact server, I tried to group the scores by id and by song_name (from the photo) but it has given no results, I tried to delete duplicates using:

DELETE t1
FROM scores t1
INNER JOIN scores t2
WHERE t1.score < t2.score
AND t1.beatmap_md5 = t2.beatmap_md5
AND t1.userid = t2.userid;

But that returned zero queries and didn't change anything at all.

SQL query that I use to gather the information:

SELECT scores.id,
       beatmaps.song_name,
       scores.beatmap_md5,
       users.username,
       scores.userid,
       scores.time,
       scores.score,
       scores.pp,
       scores.play_mode,
       scores.mods
FROM scores
LEFT JOIN beatmaps ON beatmaps.beatmap_md5 = scores.beatmap_md5
LEFT JOIN users ON users.id = scores.userid
WHERE users.privileges & 1 > 0

I really expected no duplicates to show as none of those exist, I don't know if mysql is having some caching issue or if this could be something else.


Solution

  • For avoid duplicated rows you could use distinct

        SELECT DISTINCT
            scores.id
        , beatmaps.song_name
        , scores.beatmap_md5
        , users.username
        , scores.userid
        , scores.time
        , scores.score
        , scores.pp
        , scores.play_mode
        , scores.mods 
        FROM scores 
        LEFT JOIN beatmaps ON beatmaps.beatmap_md5 = scores.beatmap_md5 
        LEFT JOIN users ON users.id = scores.userid 
        WHERE users.privileges & 1 > 0