Mysql:更新查询语法不正确

时间:2016-01-08 09:53:46

标签: mysql

我正在尝试执行此查询,但收到错误。

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);

错误是:

  

您无法指定目标表格' c'用于FROM子句中的更新

我不知道如何编写此查询以使其正常工作 如果有人有想法......
感谢

4 个答案:

答案 0 :(得分:0)

试试这样:

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

答案 1 :(得分:0)

你需要做这样的事情:

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

您无法更新表格并从子查询中的同一个表格中选择。,如here所述。

答案 2 :(得分:0)

你不需要加入这个问题;

<强> 1。方式

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。方式

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

答案 3 :(得分:0)

试试这个,您必须为子查询使用别名:

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
);

有关详情,请参阅:MySQL Error 1093 - Can't specify target table for update in FROM clause