简体   繁体   English

从另一个表更新表(更新选择)

[英]Update Table From Another Table (Update Select)

There is an application which records transactions from projects and various other data. 有一个应用程序记录来自项目和各种其他数据的交易。

There are, however, a few extra columns that are not filled it at the time of a transaction. 但是,有一些额外的列在事务处理时并未填充。 Data for these columns are housed in a separate table in database that lists all projects. 这些列的数据存储在列出所有项目的数据库的单独表中。

Example below: 下面的例子:

Transaction Table - You see in this example, the application will fill TransID, Project and Country. 交易表 -在此示例中,应用程序将填写TransID,项目和国家/地区。 However, Task and Org are not filled by app. 但是,应用程序无法填充Task和Org。

在此处输入图片说明

Project Table - This is the main projects database. 项目表 -这是主要的项目数据库。

在此处输入图片说明

Problem 问题

I need to update the transaction table 'task' and 'org' columns with data from 'project table'. 我需要使用“项目表”中的数据更新事务表“任务​​”和“组织”列。 I thought it would be something as simple as: 我认为这很简单:

UPDATE TABLE Transaction_Table A SET A.TASK =
(SELECT B.TASK FROM Project_Table B WHERE B.Project = A.Project AND B.Country = A.Country)
, A.Org = 
(SELECT B.Org FROM Project_Table B WHERE B.Project = A.Project)

Can someone please advise on best method to get result I want? 有人可以建议最好的方法以获得我想要的结果吗?

Thanks 谢谢

just use inner join between that two tables and update as usual way 只需在两个表之间使用inner联接并像往常一样更新

    UPDATE A
    set A.Task=P.Task,
        A.Org=P.Org
    from
    Transaction_Table A inner join
    Project_Table P on A.Project=P.Project and A.Country=P.Country

you can use this query in General: 您可以在“常规”中使用此查询:

UPDATE table1
SET table1.column = table2.expression1
FROM table1
INNER JOIN table2
ON (table1.column1 = table2.column1)
[WHERE conditions];

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

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