像类目,省市区删除其父ID,其子类目或者省下面的市区都删除,只需设置数据库的父ID为外键即可
如:
CREATE TABLE `shop_goods_kinds` (
`c_kindid` varchar(35) NOT NULL,
`c_kindname` varchar(35) NOT NULL,
`c_parentid` varchar(35) DEFAULT NULL,
`i_disabled` int(1) unsigned zerofill DEFAULT '0',
`c_userid` varchar(35) DEFAULT NULL,
`c_platformtype` char(2) DEFAULT NULL COMMENT '0商城;1积分馆;2抽奖商品;4企业服务超市;5诺诺服务; 6=云记账 7=政采云',
`c_kindstype` char(2) NOT NULL DEFAULT '0' COMMENT '第一层类目类型(0默认 1虚拟商品类目)',
PRIMARY KEY (`c_kindid`) USING BTREE,
KEY `shop_goods_kinds_fk1` (`c_parentid`),
CONSTRAINT `shop_goods_kinds_fk1` FOREIGN KEY (`c_parentid`) REFERENCES `shop_goods_kinds` (`c_kindid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品类别表';
将本表的c_parentid设为本表的外键,在查询的时候进行外连接查询,可获取本类目下是否还有子类目 如:
SELECT DISTINCT a.c_kindid, a.c_parentid, a.c_kindname,a.c_kindstype, CASE WHEN b.c_parentid IS NULL OR b.c_parentid = '' THEN 'N' ELSE 'Y' END AS hasChild FROM shop_goods_kinds a LEFT JOIN shop_goods_kinds b ON a.c_kindid = b.c_parentid WHERE a.i_disabled = '0'
也可以在删除某一级类目时将其子类目删除 也要注意添加某一类目时如果其外键–父ID没值也添加不了(外键的特性)