简体   繁体   English

使用另一个表作为信息查询更新表?

[英]Query to Update Table using another table as information?

I have two tables, Staff and Wages 我有两张桌子, StaffWages

Staff Contains Staff包含

 id,   name,     jobID,   wage
  1   Name1       2        
  2   Name2       4      
  3   Name3       1      
  4   Name4       2      

Wages Contains Wages包含

 JobID, Wage
   1    1500
   2    800
   3    1600
   4    2000

(There are alot more columns in the actual one I have just took the top 4) (实际上有很多列我刚刚进入前4名)

I am missing the wages inside the Staff table, and the wages I need in the staff table are the rates in the Wages table.. 我错过了Staff表内的工资, Staff表中我需要的WagesWages表中的Wages

So I need a query which would make the Staff table look like: 所以我需要一个查询,使Staff表看起来像:

 id,   name,     jobID,   wage
  1   Name1       2        800
  2   Name2       4        2000
  3   Name3       1        1500
  4   Name4       2        800

An example Query which I tried was: 我尝试的一个示例查询是:

UPDATE `Staff` 
SET wage = (SELECT wage FROM `Wages`) 
WHERE jobID = (Select jobId FROM `Wages`)

Thanks. 谢谢。

In MySQL , MySQL

UPDATE Staff a
        INNER JOIN Wages b
            ON a.jobID  = b.JobID
SET a.wage = b.wage

In MSSQL , MSSQL

UPDATE a
SET a.wage = b.wage
FROM Staff a
        INNER JOIN Wages b
            ON a.jobID  = b.JobID

I would just leave the tables as they are (without the wage column in Staff ), in their normalized state, and run this query anytime I need the full set of (denormalized) data: 我只是将它们保持原样(没有Staffwage列),处于规范化状态,并在需要完整的(非规范化)数据时随时运行此查询:

SELECT s.id, s.name, s.jobID, w.wage
FROM Staff s
LEFT OUTER JOIN Wages w ON s.jobID = w.jobID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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