Mysql Multiple Row Update Query

Discussion in 'MySQL' started by Goonline, Oct 10, 2008.

  1. #1
    Hi

    I have to update about 600 rows

    I have products id + manufacturers id for all prodcuts as sample below

    UPDATE products SET manufacturers_id= '24' WHERE products_id=2591;

    What QUERY i need to use to update rows all at once ??

    Thank you
    Good day
     
    Goonline, Oct 10, 2008 IP
  2. crivion

    crivion Guest

    Best Answers:
    0
    #2
    if you just want to set manuf. id to 24 remove the where condition and it will update every field
     
    crivion, Oct 10, 2008 IP
  3. Goonline

    Goonline Well-Known Member

    Messages:
    239
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #3
    no i need 1500 rows
     
    Goonline, Oct 10, 2008 IP
  4. Goonline

    Goonline Well-Known Member

    Messages:
    239
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #4
    And i need to make an update per product

    Thank you
     
    Goonline, Oct 10, 2008 IP
  5. ceemage

    ceemage Well-Known Member

    Messages:
    297
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Is there anything about the 600 rows you want to update that is unique to them, and no other products in the database? And is this information already in the database?

    If so, use that as your condition, e.g.

    UPDATE products SET manufacturers_id= '24'  WHERE product_type = 'woozle';
    Code (markup):
    If not, you have no alternative but to do 600 update statements. However, you can reduce the amount of typing you can do by using a spreadsheet:

    a) Type the 600 product IDs in cells A1:A600
    b) In cell B1, put a formula ="UPDATE products SET manufacturers_id= '24' WHERE product_id = '"&A1&"';"
    c) Copy formula B1 down to B600
    d) Copy the text from cells B1:B600 and paste it into your database command line tool.

    perl gurus would probably use perl to do this, but I'm a spreadsheeting kind of guy :p
     
    ceemage, Oct 10, 2008 IP
  6. Goonline

    Goonline Well-Known Member

    Messages:
    239
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #6
    I have all rows ready

    As below (i have 600 rows like it)

    UPDATE products SET manufacturers_id= '24' WHERE products_id=2593;
    UPDATE products SET manufacturers_id= '24' WHERE products_id=2592;
    UPDATE products SET manufacturers_id= '24' WHERE products_id=2591;


    But the MYSQL dosent get the UPDATE

    If i do it one by one it is ok

    If i copy all rows i get FAILEd maybe t is the Syntax
    Thank you
     
    Goonline, Oct 10, 2008 IP
  7. Goonline

    Goonline Well-Known Member

    Messages:
    239
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #7
    is there a query that will insert all rows at once ?
     
    Goonline, Oct 11, 2008 IP
  8. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #8
    This query is equavelent to above 3 queries.

    UPDATE products SET manufacturers_id= '24' WHERE products_id IN (2591, 2592, 2593);
     
    mwasif, Oct 11, 2008 IP