3

I have data on 2 tables, like this

Table 1
no|   name  |  address |
1 | alex    |  br st   | 
2 | ujang   |  cilala  |
3 | adu     |  lind st |
4 | ujang   |  bilila  |
5 | ujang   |  gea     |

Table 2
no|   name  |  address |
1 | alex    |          |
2 | ujang   |          |
3 | adu     |          |
4 | adu     |          |

My Query is like this

UPDATE TABLE1 a 
JOIN TABLE2 b ON a.name = b.name
SET a.address = b.address

Result that i want

 Table 2
    no|   name  |  address |
    1 | alex    |  br st   |
    2 | ujang   |  cilala  |
    3 | adu     |  lind st |
    4 | adu     |  lind st |

That all i don't have word to say, i'am freeze.

Sae
  • 502
  • 1
  • 4
  • 20

3 Answers3

2

The following query updates TABLE2 using a temporary table containing only records from TABLE1 with the minimum no value, in cases where a name may occur more than once in TABLE1.

UPDATE TABLE2 a
INNER JOIN
(
    SELECT t1.no, t1.name, t1.address
    FROM TABLE1 t1
    INNER JOIN
    (
        SELECT name, MIN(no) AS no
        FROM TABLE1
        GROUP BY name
    ) t2
    ON t1.name = t2.name AND t1.no = t2.no
) b ON a.name = b.name
SET a.address = b.address
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

As you mention i think you want all the addresses in table2 from tabel1,so if you want that please change the query with this one :

UPDATE TABLE1 a JOIN TABLE2 b ON a.name = b.name SET b.address = a.address
gagan mahatma
  • 336
  • 2
  • 9
0

Use like this.

UPDATE table2 
SET 
table2.address = table1.address

FROM table1, table2 
WHERE table1.name = table2.name
shiva chauhan
  • 410
  • 2
  • 7