join in SQL update statement

I had to update value from a staging table( this might be a temp table ) into another database table. Here is how I did it using a inner join in the Update statement.

UPDATE

Table1

SET

Table1

. Field1 = StagingTable . Field1

FROM

Table1

INNER JOIN StagingTable

ON Table1 . Field2 = StagingTable . Field2

WHERE

StagingTable . Field3 IS NOT NULL

---------------------------------------------------------------------

Same way we can use JOINs in delete statements

Syntax for join in Delete is a bit different e.g.

DELETE

TB1

FROM

Table1 TB1 INNERJOIN

StagingTable

_ON Table1.Field2 = StagingTable._Field2

Update 1: Following is the  MySQL syntax (Thanks Evan)

UPDATE

table1

INNER

JOIN projects

ON

table1. field1 = table2.field2

SET

table1.field3 = table2.field4

Update 2:

 This did not work for me in Oracle, had to use sub queries

Update3: Teradata

Following is the teradata way:

UPDATE TargetTable FROM SourceTable SET TargetTableField = SourceTable.SourceTableField WHERE  TargetTable.Field = SourceTable.Field; -- join on expression will come here

This article is part of the GWB Archives. Original Author: faizanahmad

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.