[英]Query to Update Table using another table as information?
I have two tables, Staff
and Wages
我有两张桌子,
Staff
和Wages
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
表中我需要的Wages
是Wages
表中的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. 谢谢。
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: 我只是将它们保持原样(没有
Staff
的wage
列),处于规范化状态,并在需要完整的(非规范化)数据时随时运行此查询:
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.