[英]Get a column data from one table and update it to another column from another table - MYSQL
I have two tables, tbl_Invoice
and tbl_tax
.我有两个表, tbl_Invoice
和tbl_tax
。
tbl_invoice
id | amount | tax
------------------
01 | 150.00 | 2.5
02 | 250.00 | 3.2
03 | 350.00 | 1.5
tbl_tax
id | inv_id | tax
-----------------
01 | 01 | 2.6
02 | 02 | 5.2
02 | 03 | 6.2
I have to update the tbl_Invoice
tax
column with the tbl_tax
tax
column, end results should be like我必须用tbl_tax
tax
tbl_Invoice
tax
最终结果应该像
tbl_invoice
id | amount | tax
------------------
01 | 150.00 | 2.6
02 | 250.00 | 5.2
03 | 350.00 | 6.2
To update such data use a subquery and fetch the tax
from you tbl_tax
table taking the common inv_id
from tbl_tax
and id
from tbl_invoice
fields.要更新此类数据,请使用子查询并从tbl_tax
表中获取tax
,从 tbl_tax 中获取常见的inv_id
,并从tbl_tax
字段中tbl_invoice
id
。
update
tbl_invoice ti
set
ti.tax=(select tt.tax from tbl_tax tt where tt.inv_id=ti.id)
Or you can use a join to update your records:或者您可以使用联接来更新您的记录:
UPDATE tbl_invoice ti
INNER JOIN `tbl_tax` tt ON tt.`inv_id`=ti.`id`
SET ti.`tax`=tt.`tax`
UPDATE tbl_invoice Inv LEFT JOIN tbl_tax Tax ON Inv.id = Tax.inv_id SET Inv.tax = Tax.tax
Reference MySQL UPDATE JOIN参考MySQL UPDATE JOIN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.