MySql - Update table using select statment from same table

前端 未结 5 483
执念已碎
执念已碎 2020-12-01 11:49

I\'m trying to update row in a table using values from a different row (and different columns) in the same table. Something along the lines of this, although my syntax produ

相关标签:
5条回答
  • 2020-12-01 12:19
    update table as t1
    inner join (
    select field_id_46,field_id_47 from table where entry_id = 36) as t2
    set t1.field_id_60 = t2.field_id_46,
        t1.field_id_61 = t2.field_id_47
    where t1.entry_id = 45
    

    or, simply

    update table as t1,
    (
    select field_id_46,field_id_47 from table where entry_id = 36) as t2
    set t1.field_id_60 = t2.field_id_46,
        t1.field_id_61 = t2.field_id_47
    where t1.entry_id = 45
    
    0 讨论(0)
  • 2020-12-01 12:22

    You are not need to this query

    SELECT field_id_46,field_id_47 FROM table WHERE entry_id = '36'
    

    You should just do this:

    UPDATE table SET (field_id_60,field_id_61) ('coming_value','other_value') WHERE entry_id = '45';
    

    Also you can do this with 2 different coloumns.I think you can do like this.But It might i havent got any idea.You should split this query in which language do you use.In first method you should use this query.

    SELECT field_id_46,field_id_47 FROM table WHERE entry_id = '36'
    

    And You can also return String that is coming from this data.Then you can use this returned value in update function.

    0 讨论(0)
  • 2020-12-01 12:27

    Adding..

    Same tables, with more of one registers

    UPDATE table t1
    INNER JOIN table t2 ON t2.entry_id = t1.entry_id
    SET t1.field_id_60 = t2.field_id_60,
        t1.field_id_61 = t2.field_id_61
    
    0 讨论(0)
  • 2020-12-01 12:27

    You can update using inner join as follow :

    UPDATE table1 AS t1 
        INNER JOIN table1 AS t2 
        SET t1.field_id_60 = t2.field_id_46, 
            t1.field_id_61 = t2.field_id_47 
    WHERE t1.entry_id = 54;
    
    0 讨论(0)
  • 2020-12-01 12:29

    I found this question very useful because I was trying to insert into a table manually while that specific database was using a hibernate_sequence table. I used the solution from this question to mod my import script. I had a script with many "insert into" statements one after the other and I had to set the id manually. for example:

    insert into T01_REGIONS (ID, NAME) VALUES ({the next id from hibernate_sequence}, 'name1');
    insert into T01_REGIONS (ID, NAME) VALUES ({the next id from hibernate_sequence}, 'name2');
    ..
    .
    

    So what I did is the following to work around my problem:

    insert into T01_REGIONS (ID, NAME) VALUES ((select next_val from hibernate_sequence limit 1), 'name1');update hibernate_sequence as t1, (select next_val+1 as next from hibernate_sequence limit 1) as t2 set t1.next_val = t2.next;
    insert into T01_REGIONS (ID, NAME) VALUES ((select next_val from hibernate_sequence limit 1), 'name2');update hibernate_sequence as t1, (select next_val+1 as next from hibernate_sequence limit 1) as t2 set t1.next_val = t2.next;
    ..
    .
    

    Adding the extra query at the end of each line of my sql script was easy with notepad++. I know this might be very ugly but it did work for me in order to import data to a test hibernate operated mysql database while my data was coming from an oracle hibernate operated database.

    0 讨论(0)
提交回复
热议问题