简体   繁体   中英

Do I need to use an insert or inner join?

I am working with MySQL version 5. I have two tables structured as shown below.

Table1

From     To     Time     TravelTime

Table2

From    To      Time     CongestionTime

I would like to achieve the following output.

If From , To and Time are equal in both the tables, then assign TravelTime = CongestionTime

Table2 contains only a subset of the From | To | Time From | To | Time From | To | Time combinations available in Table1 .

From is mysql reserved word. If you don't want to escape it, change column names "From" to "TimeFrom".

UPDATE table1,table2
SET table1.TravelTime=table2.CongestionTime
WHERE table1.From = table2.From
AND table1.To = table2.To
Update Table1 Set Table1.TravelTime = Table2.CongestionTime
FROM Table2
WHERE Table1.From = Table2.From
      AND Table1.To = Table2.To
      AND Table1.Time = Table2.Time
update table1
set traveltime = congestiontime
from table1
inner join table2 on table1.from = table2.from 
and table1.to = table2.to 
and table1.time = table2.time

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