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
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Update...case...when...then

    I'm trying to update the table [faq].
    This works ok (no order change).
    Code:
    UPDATE faq SET orderID = CASE id 
    WHEN 1 then 0 
    WHEN 2 then 1 
    WHEN 3 then 2 
    WHEN 5 then 3 
    WHEN 4 then 4 
    WHEN 6 then 5 
    ELSE orderID END
    But when I try to change the order, like this:
    Code:
    UPDATE faq SET orderID = CASE id 
    WHEN 1 THEN 0 
    WHEN 2 THEN 1 
    WHEN 3 THEN 2 
    WHEN 5 THEN 3 
    WHEN 4 THEN 4 
    WHEN 6 THEN 5 
    ELSE orderID END
    I get this error:
    Code:
    Duplicate entry '4' for key 'orderID'
    How is that?

    Thanks

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    found a solution.

    because MySQL does the update LIVE, it happens to be that duplications may accure.
    The solution is ofcourse to make the orderID index not unique.

  • #3
    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
    Wouldn't it just be easier to make the ID unique and auto-increment and then not worry about its value?

    Making an ID non-unique is usually a bad idea.


  •  

    Posting Permissions

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