You can't specify target table 'country_address' for update in FROM clause
表结构:
CREATE TABLE `country_address` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增列', `address_code` varchar(40) NOT NULL COMMENT '地区代码', `address_name` varchar(50) NOT NULL COMMENT '地区名称', `short_name` varchar(20) NOT NULL COMMENT '简称', `parent_code` varchar(40) DEFAULT NULL COMMENT '上级代码', `level` int(11) NOT NULL COMMENT '等级 1-省 2-市 3-区', `lng` varchar(20) DEFAULT NULL COMMENT '经度', `lat` varchar(20) DEFAULT NULL COMMENT '纬度', `sort` int(6) DEFAULT NULL COMMENT '排序', `created_time` datetime DEFAULT NULL COMMENT '创建时间', `updated_time` datetime DEFAULT NULL COMMENT '修改时间', `remark` varchar(250) DEFAULT NULL COMMENT '备注', `data_state` int(11) DEFAULT NULL COMMENT '状态 0-禁用 1-启用', PRIMARY KEY (`id`), KEY `Index_1` (`address_code`) ) ENGINE=InnoDB AUTO_INCREMENT=4670 DEFAULT CHARSET=utf8 COMMENT='国家省市区地址'
要禁用某个省份下的所有市和县
1、禁用省份
SELECT * FROM country_address WHERE short_name IN('*','*','*','香港','澳门','海南'); UPDATE country_address SET data_state=0 WHERE short_name IN('*','*','*','香港','澳门','海南');
2、禁用城市
SELECT * FROM country_address WHERE parent_code IN (SELECT address_code FROM country_address WHERE short_name IN('*','*','*','香港','澳门','海南')) UPDATE country_address SET data_state=0 WHERE parent_code IN ( SELECT a.address_code FROM country_address a WHERE a.short_name IN('*','*','*','香港','澳门','海南') )
这里执行update就会报错,换种方式,采用多表查询更新数据
UPDATE country_address a,country_address b SET a.data_state = 0 WHERE a.parent_code = b.address_code AND b.short_name IN ('*','*','*','香港','澳门','海南')
3、禁用区县
下面同理,换种方式更新同一张表数据就可以了
SELECT c.* FROM country_address a,country_address b,country_address c WHERE c.parent_code=b.address_code AND b.parent_code=a.address_code AND a.short_name IN ('*','*','*','香港','澳门','海南') UPDATE country_address a,country_address b,country_address c SET c.data_state = 0 WHERE c.parent_code=b.address_code AND b.parent_code=a.address_code AND a.short_name IN ('*','*','*','香港','澳门','海南')