0

I am trying to execute this query but I am getting an error.

update T_CLIENT c set taxe_income = true
where c.id in (select c1.id from T_CLIENT c1, T_ADRESS a
where c1.id = a.client_id and a.country_id = 14  and a.adress_principale is true);

The error is :

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

I don't know how to write this query in order to make it work.
If anyone has an idea...
Thanks

user1260928
  • 3,129
  • 6
  • 57
  • 99

4 Answers4

0

Try like this:

update T_CLIENT c inner join T_ADRESS a on c.id = a.client_id 
set taxe_income = true
where a.country_id = 14  and a.adress_principale is true
Rahul Tripathi
  • 161,154
  • 30
  • 262
  • 319
0

You need to do something like this:

UPDATE table SET col = (
  SELECT ... FROM (SELECT.... FROM) AS t);

You cannot update a table and select from the same table in a subquery. as stated here.

teoreda
  • 2,186
  • 1
  • 19
  • 26
0

You don't need join for this question;

1. way

update T_CLIENT c,T_ADRESS a  set c.taxe_income = true
    where c.id = a.client_id 
    and a.country_id = 14  
    and a.adress_principale is true

2. way

UPDATE T_CLIENT
SET T_CLIENT.taxe_income  = true
    FROM T_CLIENT c,T_ADRESS a
        WHERE c.id = a.client_id 
        and a.country_id = 14  
        and a.adress_principale is true
hurricane
  • 6,176
  • 2
  • 31
  • 42
0

Try this, you have to use an alias for your subquery :

update T_CLIENT c set taxe_income = true
where c.id in (
    (
    select c1.id from T_CLIENT c1, T_ADRESS a
    where c1.id = a.client_id and a.country_id = 14  and a.adress_principale is true
    ) as tmptable
);

For more information, read this : MySQL Error 1093 - Can't specify target table for update in FROM clause

Community
  • 1
  • 1
kmas
  • 6,301
  • 13
  • 38
  • 61