mysql find_in_set函数以及group_concat

找到子部门(Mysql)

BEGIN
	DECLARE sTemp VARCHAR(500);
	DECLARE sTempChd VARCHAR(500);
	SET sTemp = '';
	SET sTempChd =parId;
	WHILE sTempChd is not null DO
		SET sTemp = concat(sTemp,',',sTempChd);
		SELECT group_concat(department_code) INTO sTempChd FROM department where FIND_IN_SET(upper_department_code ,sTempChd)>0;
	END WHILE;
	RETURN sTemp;
END

另外今天才知道MYSQL GROUP_CONCAT支持内部排序,涨知识了...

SELECT
	GROUP_CONCAT(
		CASE
		WHEN leave_type IS NULL THEN
			NULL
		ELSE
			CONCAT(
				RIGHT (date_id, 4),
				':请',
				(
					SELECT
						value_chinese_name
					FROM
						common_parameter
					WHERE
						collection_code = 'QJLX'
					AND value_code = a.leave_type
				)
			)
		END
		ORDER BY
			date_id
	) comments
FROM
	attendance a;

你可能感兴趣的:(数据库)