SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    UPDATE multiple rows with different IDs

    I am trying to figure out how to run an update to multiple rows by using their ID, for example if I have the following query I am updating based on the state and all matching rows will be affected, but what if I wanted to run that same query but based on IDs

    UPDATE sometable SET state = 0 WHERE state = 1

    So I want to do something like

    UPDATE sometable SET state = 0 WHERE state = 1 AND id = 1

    but I want to run the query in multiple items like IDs 1,2,3,4,20,45... etc

    Any help is appreciated
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think I found the way, I will test it but if I am wrong please let me know, I will do

    UPDATE sometable SET state = 0 WHERE state = 1 AND id = IN ($ids)

    Where $ids is an array I will create based on the IDs I want to update

    UPDATE: I tested that and it did work, actually instead of having a variable I did it like this just to test

    UPDATE #__tlakcalendarcategories SET state = 1 WHERE state = 0 AND id IN (1,2,3,4,5,6)
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the way I would do it


Bookmarks

Posting Permissions

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