Need Help: Simple MySql Querry.

Discussion in 'MySQL' started by YIAM, Jul 13, 2007.

  1. #1
    I have two tables:

    Table_1
    --------------

    ID | NAME | AMOUNT
    1 | name1 | -
    2 | name2 | -
    3 | name3 | -
    4 | name4 | -
    5 | name5 | -


    Table_2

    ID | NAME_ID | AMOUNT
    1 | 2 | 5
    2 | 3 | 4
    3 | 5 | 12

    I want to copy amount from table_2 to table_1 where name_id of table_2 and id of table_1 are same. After updating table_1 should look like this:

    Table_1
    --------------

    ID | NAME | AMOUNT
    1 | name1 | -
    2 | name2 | 5
    3 | name3 | 4
    4 | name4 | -
    5 | name5 | 12

    Need your help urgently.
    Thanks in advance.
     
    YIAM, Jul 13, 2007 IP
  2. Darc

    Darc Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    so let me make sure i get this right.... all you want to do is take the data fields from Table_2 under the section called Amount and transfer them to Table_1 ? How much data do you have to transfer?
     
    Darc, Jul 13, 2007 IP
  3. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Yes you are right.

    I found the following code is working and tested on small table (20 rows.)

    UPDATE table_1,table_2 SET table_1.amount=table_2.amount WHERE table_1.id=table_2.name_id

    Do you think this will be a problem if i have 1000+rows in database.
     
    YIAM, Jul 13, 2007 IP
  4. Synch

    Synch Peon

    Messages:
    76
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    nope, a thousand rows is a very small amount compared to some bigger databases.
     
    Synch, Jul 13, 2007 IP
  5. Darc

    Darc Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    As long as you are willing to set the values for each line in the query... This will work for any amount of rows. I work with small databases (less than 400 tables), and some larger databases. The biggest database I edit is somewhere around 5700 tables... god knows how many rows is in that thing.

    The only thing that you will have to watch is your file upload limit... most databases allow the sql files to be about 50mb, some may not allow more than 2mb at a time. Even if you have to break up the queries, if you take the time you can add anything. I have to edit and update 14,000 rows for one table in the next few days.
     
    Darc, Jul 14, 2007 IP