从不同查询的结果更新列

时间:2017-04-22 10:04:32

标签: mysql

我有两张桌子(车辆和费用),我在费用表中引入了一个部门栏目 所以我想更新费用表中的部门列与车辆表中的部门

我的车辆表是这样的:

vehicle Department
S526    424
D097    402
D113    403
D111    430
C005    400
C003    403
C002    403

和成本tvle是这样的:

car     Dept
D097    
C005    
D113    
D113    
D111    
C005    
D113    
C005    
C003    
C002    
C005    
S526    
C002    
C002

如何更新费用表以包含相关部门

我试过这个但是没有用:

update costs as h,
(
  select v.vid as 'car',
  v.department as 'department',
  h.VID as 'newcar'
  from vehicles AS v
  join costs as h on v.vid = h.VID and v.costcentre = h.CostCenter
) as t2
set h.department = t2.department

1 个答案:

答案 0 :(得分:0)

update costs h,vehicles v
set h.Dept = v.Department
where v.vehicle = h.car;