It’s possible to make a query to see if there is duplicated records in the same row?
I tried to find a solution but all I can find is to detected duplicated fields in columns, not in rows.
example, let’s say I have a table with rows and items:
| id | item1 | item2 | item3 | item4 | item5 | upvotes | downvotes |
--------------------------------------------------------------------
| 1 | red | blue | red | black | white | 12 | 5 |
So I want to see if is possible to make a query to detect the fields with the same record (in this case red) and delete them, and/or how to redesign my structure to not allow the duplicates.
Thanks.
With redesign:
You may redesign the layout to something like this:
With (id, item) as primary key, this will forbid having twice the same
itemfor a givenid.So the data will looks like this:
Trying to insert a
| 1 | red |again will throw an error.Without redesign:
If you don’t want to change the layout, this query will find rows in which one of the
fieldXfield equals an otherfieldXof the same row:(Assuming you have 5 of those
fieldXcolumns.)This actually counts the different
fieldXvalues for eachid. If the count is different than the number of fields, then there is a duplicate. The query returns the ids of the rows in which there is a duplicate.After testing you can delete rows with