叙述
mysql根据查询结果执行批量更新或插入时经常会遇到1093的错误问题。基本上批量插入或新增都会涉及到子查询,mysql是建议不要对需要操作的表放入子查询条件中的,因此我们尽量避免子查询中涉及到需要操作的表,如果无法避免,则可以考虑用连接查询的方式进行。
ERROR 1093 (HY000): You can't specify target table 'dir' for update in FROM clause
根据查询结果批量更新
update
file_directory_info dir left join
file_directory_temp dirTempon
dirTemp.pk_id > 1000
and
dirTemp.dir_category = dir.dir_categoryset dir.parent_id = dirTemp.pk_id, dir.dir_level = 2, dir.dir_seq = concat('.',dirTemp.pk_id, dir.dir_seq)where dir.dir_level is null;
根据查询结果批量新增
insert into
file_directory_info
(pk_id, dir_category, dir_seq, dir_name, parent_id)
select
(dictid + 1000) as pk_id,
dictid as dir_category,
concat('.',(dictid + 1000),'.') as dir_seq,
dictname as dir_name,
0 as parent_id
from dict_entry dict
where dict.dictTypeid = 'HTCM_FILE_CLASSIFICTION' ;