简体   繁体   中英

How to update a field of a table by using data from another table(MYSQL)

I have two tables, mileage_registrants and date_import. Both tables have field 'user_id' and 'department'. What I want to do is to update department of mileage_registrants with department info from table data_import by matching the user_id of both tables.

The query I got is wrong. How to write the correct query? thanks

Update mileage_registrants
SET mileage_registrants.department = test_date_import.department
INNER JOIN test_date_import
ON(test_date_import.user_id = mileage_registrants.user_id)

This works in SQL Server, and should work in MySQL:

UPDATE 
  mileage_registrants
SET 
  m.department = t.department
FROM 
  mileage_registrants m
INNER JOIN 
  test_date_import t
ON 
  t.user_id = m.user_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM