Step 1: set global log_bin_trust_function_creators = TRUE; if ERROR 1418 (HY000): This function has none of DETERMINISTIC, no SQL, or reads SQL DATA in its declaration and binary logging is enabled (you * might * want to use the less safe log_bin_trust_function_creators variable) the second step of www.2cto.com: SQL code DELIMITER $ USE 'zhiku '$ DROP FUNCTION IF EXISTS 'getchilddept' $ Create function 'getchilddept' (rootId INT) returns text charset utf8 begin declare sTemp VARCHAR (1000); DECLARE sTempChd VARCHAR (1000); SET sTemp = '$ '; SET sTempChd = CAST (rootId as char); WHILE sTempChd is not null do set sTemp = CONCAT (sTemp, ',', sTempChd); SELECT GROUP_CONCAT (id) INTO sTempChd FROM zk_administrative ments WHERE FIND_IN_SET (parent_id, sTempChd)> 0; end while; RETURN sTemp; END $ DELIMITE R; Step 3: directly call select distinct (d. user_id) AS user_id, d. dept_id, u. compellation FROM zk_user_ments ments d inner join zk_users u ON u. id = d. user_id and instr (u. pinyin, 'H') = 2 WHERE FIND_IN_SET (d. dept_id, FIG (128) group by d. user_id; put in cakephp: Php code $ conditions = array ('Find _ IN_SET (dept_id, getChildDept ('. $ dept_id. '); $ condition_join = 'user '. 'id' = 'userdepartment '. 'User _ id'; if (! Emptyempty ($ c) $ condition_join. = 'and instr (User. pinyin ,"'. $ c. '") = 2'; // page $ this-> paginate = array ('userdepartment' => array ('conditions' => $ conditions, 'order' => array ('dept _ id' => 'asc'), 'limit' => 10, 'recursion' =>-1, 'group' => array ('user _ id'), 'fields' => array ('user _ id', 'dept _ id '), 'join' => array ('Alias' => 'user', 'table' => 'zk _ users', 'type' => 'inner ', 'condition' => $ condition_join,),); $ data = $ this-> paginate ('userdepartment ');