[关闭]
@Chiang 2021-09-12T08:37:51.000000Z 字数 1603 阅读 462

树形结构的数据库表设计 & MYSQL 传汉字获取拼音首字母

2021-09 树形表 拼音首字母


树形结构的数据库表设计

  1. CREATE FUNCTION `queryChildrenAreaInfo` (Id INT)
  2. RETURNS VARCHAR (4000)
  3. BEGIN
  4. DECLARE sTemp VARCHAR(400);
  5. DECLARE sTempChd VARCHAR(400);
  6. SET sTemp = '$';
  7. SET sTempChd = cast(Id as char);
  8. WHILE sTempChd is not NULL DO
  9. SET sTemp = CONCAT(sTemp,',',sTempChd);
  10. SELECT group_concat(org_Id) INTO sTempChd FROM sys_organise where FIND_IN_SET(org_parentId,sTempChd)>0;
  11. END WHILE;
  12. return sTemp;
  13. END
  14. # 调用方式:select queryChildrenAreaInfo(1);
  15. select * from t_areainfo where FIND_IN_SET(id, queryChildrenAreaInfo(20));

MYSQL 传汉字获取拼音首字母

  1. CREATE DEFINER=`by`@`%` FUNCTION `fun_pinyin`(`P_NAME` VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
  2. NO SQL
  3. BEGIN
  4. DECLARE V_COMPARE VARCHAR(255);
  5. DECLARE V_RETURN VARCHAR(255);
  6. DECLARE I INT;
  7. SET I = 1;
  8. SET V_RETURN = '';
  9. while I <= char_length(P_NAME) do
  10. SET V_COMPARE = SUBSTR(P_NAME, I, 1);
  11. IF (V_COMPARE != '') THEN
  12. SET V_RETURN = CONCAT(V_RETURN, fun_first_pinyin(V_COMPARE));
  13. END IF;
  14. SET I = I + 1;
  15. end while;
  16. IF (ISNULL(V_RETURN) or V_RETURN = '') THEN
  17. SET V_RETURN = P_NAME;
  18. END IF;
  19. RETURN V_RETURN;
  20. END
  21. CREATE DEFINER=`by`@`%` FUNCTION `fun_first_pinyin`(`P_NAME` VARCHAR(5)) RETURNS varchar(5) CHARSET utf8
  22. NO SQL
  23. BEGIN
  24. DECLARE V_RETURN VARCHAR(5);
  25. SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT(P_NAME USING gbk),1)),16,10),
  26. 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
  27. 0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,
  28. 0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
  29. 'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
  30. IF ISNULL(V_RETURN) or V_RETURN = '' THEN
  31. RETURN '';
  32. ELSE
  33. RETURN V_RETURN;
  34. END IF;
  35. END

参考资料:
数据库树形表结构如何查询
MYSQL 传汉字获取拼音首字母
树形权限表的设计
树形结构表3种设计分析分享
树形结构的数据库表设计

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注