S
S
sendnikkei2018-04-16 19:53:45
MySQL
sendnikkei, 2018-04-16 19:53:45

How to change values ​​in one table by taking values ​​from another?

Hi guys! Tell the fool how it is possible to implement the following.
There is a database. There are 2 tables in the database (below: name - structure):
1. tab.1 - || second name | last name | third name | key ||
2.tab.2 - || second name | last name | Third Name ||
I need to take records from tab.2 with values: 'SecondName', 'LastName', 'ThirdName', and in tab.1 for these records, update the Key value.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexalexes, 2018-04-16
@sendnikkei

And where are the primary keys in the tables?
In general, if the tables are some copies of each other, then the updates of the records are handled something like this.

update tab1 t1, tab2 t2   // таблицы, участвующие в Update
set  t1.key = ?                      // устанавливаем параметр key, только непонятно, что задаем
where t1.id = t2.id          // соответствие записей по первичным ключам

If you need to make a complex update by matching a certain set of attributes without being tied to keys, then something like this.
update tab1 t1, tab2 t2   // таблицы, участвующие в Update
set t1.key = ?                      // устанавливаем параметр key, только непонятно, что задаем
where t1.SecondName = t2.SecondName // допустим, пусть будет обновление key у тех записей, у которых есть полное соответствие атрибутов.
   and t1.LastName = t2.LastName
  and t1.ThirdName = t2.ThirdName

D
d-stream, 2018-04-17
@d-stream

Generalized universal version:

update table
set ...
from table
join ....
where ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question