48

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 produces no results: Here is the code (updated):

UPDATE table1 AS t1 INNER JOIN
(SELECT field_id_46,field_id_47 FROM table1 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;

6 Answers 6

78
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
Sign up to request clarification or add additional context in comments.

4 Comments

This looks like it should work, but it doesn't (No results). My exact query is:UPDATE table1 AS t1 INNER JOIN (SELECT field_id_46,field_id_47 FROM table1 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;
Ah yes, it was my CMS not behaving properly. Thanks for your help.
Amazing job @Nicola... With explain we can even tune the alternatives. Many thanks
this type of queries working with MySQL version 5.5, 5.6 but not with 5.7 or 8
24

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

1 Comment

Of course the word "table" should be substituted with your table name. Don't actually put the word "table" in your SQL. (Don't ask me why I felt the need to mention that)
11

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;

Comments

0

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.

Comments

0
update tableX set fieldM = x
where id in
    (select inner1.idOfTable 
    from (
        select tb.id as idOfTable 
        from tableX tb 
        where someCondition
    ) inner1);

Comments

-1

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.

1 Comment

This way needs 2 queries separateds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.