I have a table with year column and this column shouldn’t have duplicate values. So I end up with a table with only one 2007 year record for example.
So how could I delete those rows that have duplicate year value?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think you could simply try adding a UNIQUE INDEX using IGNORE:
MySQL should respond with something like:
Of course, you’ll leave it up to MySQL to decide which rows to drop.
EDIT:
this works for as many columns as you like:
check MySQL’s documentation on CREATE INDEX. A common gotcha (at least one I ran into once) is to forget that
NULL = NULLisn’t true (butNULL), hence {42, NULL} and {42, NULL} are allowed for a UNIQUE index on two columns.