SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Updates

    Hello,

    I have three separate UPDATE command but I want them as a single command.
    Code MySQL:
    Update `backgroundFiles` SET dimension = "1024x768" where medProfID=4172
    Update `backgroundFiles` SET dimension = "1620x1200" where medProfID=4173
    Update `backgroundFiles` SET dimension = "1280x1024" where medProfID=4174
    How can I combine above three command in a single UPDATE command?

    Thanks

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    UPDATE backgroundFiles 
       SET dimension = 
           CASE WHEN medProfID = 4172
                THEN '1024x768'
                WHEN medProfID = 4173
                THEN '1620x1200'
                WHEN medProfID = 4174
                THEN '1280x1024'
                ELSE NULL END
     WHERE medProfID IN ( 4172,4173,4174 )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you r937,

    It is working but I did not understand why we need to specify again the WHERE values as an another array although they were used in conditionals?

    WHERE medProfID IN ( 4172,4173,4174 )

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    the WHERE clause is vitally important

    try leaving it off and you will see why

    tip: back up your table first

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •