在mysql 8 使用 CTE递归查询时,出现了这个报错
WITH recursive area AS ( SELECT area_name, area_code FROM sys_area_tree WHERE area_category = '1' AND parent_code IS NULL UNION ALL SELECT concat( t1.area_name, '/', t.area_name ), t.area_code FROM sys_area_tree t INNER JOIN area t1 ON t.parent_code = t1.area_code AND t.area_category = '1' ) select * from area
原因:
是因为参与递归concat的【area_name】字段超出了长度限制,也就是说在非递归语句中,一开始【area_name】字段的长度就确定了
解决方案:
在cte表达式中非递归的查询语句中,将初始字段的长度重新转换一下,cast(xxx as CHAR(1024)) area_name
将sql 修改为:
WITH recursive area AS ( SELECT cast( area_name AS CHAR ( 1024 )) area_name, area_code FROM sys_area_tree WHERE area_category = '1' AND parent_code IS NULL UNION ALL SELECT concat( t1.area_name, '/', t.area_name ), t.area_code FROM sys_area_tree t INNER JOIN area t1 ON t.parent_code = t1.area_code AND t.area_category = '1') select * from area
参考:https://dev.mysql.com/doc/refman/8.0/en/with.html
标签:code,name,递归,area,xxx,long,t1,SELECT,colum From: https://www.cnblogs.com/wongzzh/p/17782256.html