mysql从另一个表更新一个列

时间:2022-02-17 17:06:10
Table1:
id  name  value
===================
1   Jane   28
2   Joe    35

Table2:
id  name  value  integer
=========================
1   Jane    28      379
2   Joe     30      325
2   Joe     32      380
1   Jane    28      385

Table1 contains information about user: ID, Name and Value.
Table2 contains information about changes: ID, Name, Value and Integer.
Integer is a value thats always increase and changes about twice a day.

表1包含有关用户的信息:ID,名称和值。表2包含有关更改的信息:ID,名称,值和整数。整数是一个总是增加的值,每天变化两次。

I want to transfer the value from table1 for a specific ID and that value to table2 and put that value at the last line.

我想将table1中的值传递给特定的ID,将该值传递给table2,并将该值放在最后一行。

In this example I want ID: 2 Value: 35 in Table1 to relace

在这个例子中我想要ID:2值:表1中的35来重新发送

ID: 2 Value: 32 in Table2. And I need to use the Integer in some way..

ID:2值:表2中的32。我需要以某种方式使用Integer ..

1 个解决方案

#1


If interger the column which tells the latest in the table2 then you can use the update join as

如果在表2中输入告知最新的列,则可以使用更新连接作为

update table1 t1 
join table2 t2 on t1.id = t2.id 
join ( 
  select max(`integer`) as `integer`,id 
  from table2 
  group by id
)x 
on x.id = t2.id and x.`integer` = t2.`integer` 
set t1.value = t2.value 
where t1.id = 2 ;

#1


If interger the column which tells the latest in the table2 then you can use the update join as

如果在表2中输入告知最新的列,则可以使用更新连接作为

update table1 t1 
join table2 t2 on t1.id = t2.id 
join ( 
  select max(`integer`) as `integer`,id 
  from table2 
  group by id
)x 
on x.id = t2.id and x.`integer` = t2.`integer` 
set t1.value = t2.value 
where t1.id = 2 ;