在1台服务器上的2个不同数据库中的表之间使用INNER JOIN更新查询

时间:2012-03-27 17:12:14

标签: sql-server database syntax inner-join

需要一些SQL语法帮助: - )

两个数据库都在同一台服务器上

db1 = DHE
db2 = DHE_Import

UPDATE DHE.dbo.tblAccounts 
INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink 
    ON DHE.dbo.tblAccounts.AccountCode = DHE_Import.tblSalesRepsAccountsLink.AccountCode 
SET DHE.dbo.tblAccounts.ControllingSalesRep = DHE_Import.dbo.tblSalesRepsAccountsLink.SalesRepCode

我可以在Access中使用具有类似语法的链接表进行查询 - 但是SQL不喜欢它。

我确定这是一个简单的问题:-D

谢谢!

11 个答案:

答案 0 :(得分:95)

您可以将其称为样式,但我更喜欢使用别名来提高可读性。

UPDATE A    
  SET ControllingSalesRep = RA.SalesRepCode   
from DHE.dbo.tblAccounts A
  INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink RA
    ON A.AccountCode = RA.AccountCode

对于MySQL

UPDATE DHE.dbo.tblAccounts A 
  INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink RA 
      ON A.AccountCode = RA.AccountCode 
SET A.ControllingSalesRep = RA.SalesRepCode

答案 1 :(得分:26)

以下是MySQL语法:

UPDATE table1 
INNER JOIN table2 ON table1.field1 = table2.field2
SET table1.field3 = table2.field4 
WHERE ...... ;

http://geekswithblogs.net/faizanahmad/archive/2009/01/05/join-in-sql-update--statement.aspx

答案 2 :(得分:5)

很抱歉它迟到了,但我想这对那些在这里寻找类似问题解决方案的人有所帮助。 set子句应该在update子句之后。因此,通过稍微更改重新排列查询就可以了。

UPDATE DHE.dbo.tblAccounts 
SET DHE.dbo.tblAccounts.ControllingSalesRep
    = DHE_Import.dbo.tblSalesRepsAccountsLink.SalesRepCode
from DHE.dbo.tblAccounts 
INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink 
    ON DHE.dbo.tblAccounts.AccountCode
        = DHE_Import.tblSalesRepsAccountsLink.AccountCode 

答案 3 :(得分:4)

UPDATE table1 a
 inner join  table2 b on (a.kol1=a.b.kol1...)
SET a.kol1=b.kol1
WHERE 
a.kol1='' ...

对我来说,直到语法工作-MySQL

答案 4 :(得分:3)

应该是这样的:

UPDATE DHE.dbo.tblAccounts
   SET DHE.dbo.tblAccounts.ControllingSalesRep = 
       DHE_Import.dbo.tblSalesRepsAccountsLink.SalesRepCode
  from DHE.dbo.tblAccounts 
     INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink 
        ON DHE.dbo.tblAccounts.AccountCode =
           DHE_Import.tblSalesRepsAccountsLink.AccountCode 

在FROM子句中重复更新表。

答案 5 :(得分:1)

可能有用

Update
    A INNER JOIN B ON A.COL1=B.COL3
SET
    A.COL2='CHANGED', A.COL4=B.COL4,......
WHERE ....;

答案 6 :(得分:0)

这里解释http://erabhinavrana.blogspot.in/2014/01/how-to-execute-update-query-by-applying.html

它还有其他常用的有用代码片段。

update <dbname of 1st table>.<table name of 1st table> A INNER JOIN <dbname of 2nd table>.<table name of 2nd table> RA ON A.<field name of table 1>=RA.<field name of table 2> SET A.<field name of table 1 to be updated>=RA.<field name of table 2 to set value in table 1>

<>中的数据替换为适当的值。

就是这样。 来源:

  

http://www.dynamic-coders.com/how-to-update-two-different-tables-in-different-databases-on-same-server

答案 7 :(得分:0)

//For Access Database:
UPDATE ((tblEmployee
LEFT JOIN tblCity ON (tblEmployee.CityCode = tblCity.CityCode))
LEFT JOIN tblCountry ON (tblEmployee.CountryCode = tblCountryCode))
SET tblEmployee.CityName = tblCity.CityName, 
tblEmployee.CountryName = tblCountry.CountryName
WHERE (tblEmployee.CityName = '' OR tblEmployee.CountryName = '')

答案 8 :(得分:0)

strip_tags(text)

答案 9 :(得分:0)

为我完美地工作。

UPDATE TABLE_A a INNER JOIN TABLE_B b ON a.col1 = b.col2 SET a.col_which_you_want_update = b.col_from_which_you_update;

答案 10 :(得分:0)

使用SQL中的内部联接查询来更新非常简单。您可以使用 FROM子句来做到这一点。这是一个示例:

    UPDATE customer_table c 

      INNER JOIN  
          employee_table e
          ON (c.city_id = e.city_id)  

    SET c.active = "Yes"

    WHERE c.city = "New york";