代码改变世界

Mysql语法

2023-04-20 16:18  火的天堂  阅读(9)  评论(0编辑  收藏  举报

树形结构通过节点获取节点所有上级(函数)

 1 create function get_department_list(in_id int) returns varchar(100)
 2 READS SQL DATA
 3 begin
 4     declare ids varchar(1000);
 5     declare tempid int;
 6       
 7     set tempid = in_id;
 8     while tempid > 0 do
 9         set ids = CONCAT_WS(',',ids,tempid);
10         select ParentId into tempid from department where id=tempid;
11     end while;
12     return ids;
13 end

GROUP_CONCAT

把某个字段的数据显示到一列以逗号隔开

REPLACE

替换