mysql更新从选择-相同的表

时间:2021-04-09 00:11:01

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;

5 个解决方案

#1


37  

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

#2


7  

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

#3


2  

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;

#4


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:

我发现这个问题非常有用,因为当特定的数据库使用hibernate_sequence表时,我试图手动插入到一个表中。我使用了这个问题的解决方案来修改我的导入脚本。我有一个脚本,其中有许多“插入”语句,而我必须手动设置id。例如:

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.

在我的sql脚本的每一行末尾添加额外的查询对于notepad++来说都很简单。我知道这可能很难看,但它确实对我有用,当我的数据来自oracle hibernate操作的数据库时,它可以将数据导入hibernate操作的mysql数据库。

#5


-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.

你也可以用两个不同的coloumns。我想你可以这样做。但我可能不知道。您应该将该查询拆分为使用哪种语言。在第一种方法中,您应该使用这个查询。

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.

你也可以返回来自这个数据的字符串。然后可以在update函数中使用这个返回值。

#1


37  

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

#2


7  

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

#3


2  

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;

#4


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:

我发现这个问题非常有用,因为当特定的数据库使用hibernate_sequence表时,我试图手动插入到一个表中。我使用了这个问题的解决方案来修改我的导入脚本。我有一个脚本,其中有许多“插入”语句,而我必须手动设置id。例如:

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.

在我的sql脚本的每一行末尾添加额外的查询对于notepad++来说都很简单。我知道这可能很难看,但它确实对我有用,当我的数据来自oracle hibernate操作的数据库时,它可以将数据导入hibernate操作的mysql数据库。

#5


-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.

你也可以用两个不同的coloumns。我想你可以这样做。但我可能不知道。您应该将该查询拆分为使用哪种语言。在第一种方法中,您应该使用这个查询。

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.

你也可以返回来自这个数据的字符串。然后可以在update函数中使用这个返回值。