0

I have column in mysql table (my_sale_time) of type timestamp....infact the rows value look like this

2010-12-01 14:38:07
2010-12-01 17:14:18
...

so what i need mysql query to delete those value whose date is repeated multiple times in table.....as i mentioned in sample sale_time_value.....only date is repeated with same value but time is different....so i want to get all rows, date is repeated multiple times and delete duplicate dates

3
  • First you need to decide the criteria for which row of the duplicates you want to keep since all columns are not exact dupes. Commented Feb 4, 2011 at 16:06
  • I assume all other columns are identical and it's only the time part of the timestamp that is different? Commented Feb 4, 2011 at 16:08
  • do you have an auto-increment column on the table in question that the dups exist? Commented Feb 4, 2011 at 16:22

2 Answers 2

7

The basic principle of deleting duplicate rows:

CREATE TEMPORARY TABLE tmptbl AS SELECT DISTINCT * FROM my_sale_time;
DELETE FROM my_sale_time;
INSERT INTO my_sale_time SELECT * FROM tmptbl;

You may have to specify columns and WHERE clauses (I didn't really understand your criteria). And of course you should test-run it on a development server and don't forget to run it as a single transaction with locked tables.

Sign up to request clarification or add additional context in comments.

5 Comments

okay i will tell you what i need,,,,,i have column 'sale_time' and it has dateandtime values stored.....so, what i want is, i want to delete only date which is repeated more then 1,,,,,here is the example : 2010-12-01 14:38:07,2010-12-01 17:14:18. if you notice to this values, u can find date is same but time is different so any how i want to delete this type of values from table...how?
Run CREATE TEMPORARY TABLE tmptbl AS SELECT DISTINCT DATE(sale_time), * FROM my_sale_time and check if tmptbl contains the rows you want.
okay i will tell you what i need,,,,,i have column 'sale_time' and it has dateandtime values stored.....so, what i want is, i want to delete only date which is repeated more then 1,,,,,here is the example : 2010-12-01 14:38:07,2010-12-01 17:14:18. if you notice to this values, u can find date is same but time is different so any how i want to delete this type of values from table...how?
you have your answer. Don't start with "feed me cookies now"
@shahid: Please stop posting the same comment again. It doesn't make things clearer!
0

If you have an auto_increment field, use this:

DELETE FROM
    `mytable`
WHERE
    `my_auto_increment_field` NOT IN (
        SELECT
            MAX(`my_auto_increment_field`)
        GROUP BY
            `my_sale_time`
    );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.