Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update table using alias

Tags:

sql

mysql

I need to fill some fields in a table getting informations from other records of the same table. I tried to write a query to explain what I want to do:

update globale2
set
  nita = t.nita,
  tita = t.tita,
  notaita = t.notaita
where
  neng = t.neng and
  nita is null
  (select nita, neng, tita, notaita from globale where uris='mma' and nita is not null) as t

edit to eplain better:

every records have these fields: "nita", "tita", "notaita", "neng" ("neng" cannot be null)

I want to fill these fields: "nita", "tita", "notaita" (where "nita" is empty) with the same values from another record where "neng" equals the other "neng"

like image 809
Silvestro Magliola Avatar asked Dec 04 '13 12:12

Silvestro Magliola


1 Answers

You can however, join the two tables.

UPDATE  globale2 g
        INNER JOIN globale gg
            ON g.neng = gg.neng
SET     g.nita = gg.nita,
        g.tita = gg.tita,
        g.notaita = gg.notaita
WHERE   g.nita IS NULL
        AND gg.uris = 'mma' 
        AND gg.nita IS NOT NULL
like image 160
John Woo Avatar answered Sep 30 '22 00:09

John Woo