Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    113
    Thanks
    6
    Thanked 1 Time in 1 Post

    Please help with deleting "every other row" in mysql table

    What I'm trying to do is simple; I need to delete every other row in a mysql table.
    skip row, delete, skip row, delete, skip row, delete ...

    For example this table:

    id | name
    ---------------
    1 | John
    2 | Bob
    3 | Mary
    4 | Alex
    5 | Tom
    6 | Pat


    After delete, becomes this:

    id | name

    ---------------
    1 | John
    3 | Mary
    5 | Tom


    Any ideas?
    Last edited by eapro; 09-04-2009 at 10:55 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Assuming your "id" column is a continuous sequence of numbers, check the modulus of the id divided by 2 and delete when it's 0.

    Code:
    DELETE FROM table1
    WHERE ID % 2 = 0

  • Users who have thanked Fumigator for this post:

    eapro (09-04-2009)

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Posts
    113
    Thanks
    6
    Thanked 1 Time in 1 Post
    That worked perfectly, thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •