user1368392
user1368392

Reputation: 489

MySql - Update table using select statment from same table

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;

Upvotes: 48

Views: 115845

Answers (6)

Serdar Turan
Serdar Turan

Reputation: 1

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

Upvotes: 0

xtsoler
xtsoler

Reputation: 1800

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.

Upvotes: 0

Diego
Diego

Reputation: 271

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

Upvotes: 24

krishna Prasad
krishna Prasad

Reputation: 3812

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;

Upvotes: 11

Nicola Cossu
Nicola Cossu

Reputation: 56357

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

Upvotes: 78

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.

Upvotes: -1

Related Questions