1

From my perspective this query should work

UPDATE tab_antiguedad set rol_id = 4 
where ant_id = 29 
      and rol_id not in (
                         select rol_id 
                         from tab_antiguedad 
                         where usu_id = 55
                        )

Where (select rol_id from tab_antiguedad where usu_id = 55) here rol_id has 4 and 3.

But the mistake that throws me is:

You can't specify target table 'tab_antiguedad' for update in FROM clause

Please help.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68

2 Answers2

0

The thing is that you cannot update the same table you used in the select part.

UPDATE tab_antiguedad

set rol_id = 4

where ant_id = 29 and

rol_id not in (select rol_id from tab_antiguedad where usu_id = 55)

Pic
  • 139
  • 1
  • 8
  • So what would be the solution? – Wilmer Hilaquita Jun 02 '16 at 14:18
  • I do not know much about subqueries, since I mainly use simple SQL commands, but I found a similiar question which I think that can help you. Just click [THIS](http://stackoverflow.com/questions/13283940/select-query-in-where-clause-of-update-query?lq=1) to open it. – Pic Jun 02 '16 at 14:53
0

Try using alias

UPDATE tab_antiguedad as t1, (select rol_id 
        from tab_antiguedad   where usu_id = 55) as t2
set t1.rol_id = 4 
where t1.ant_id = 29 
and t1.rol_id != t2.rol_id 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107