2017年5月

在一个需求场景中,要实现对标签和专题首字母的索引.在网页上展示A-Z.
然后点对应字母显示相应的数据.
实现过程笔记如下
创建函数

CREATE FUNCTION `fristPinyin`(P_NAME VARCHAR(5000)) RETURNS varchar(255) CHARSET utf8
DETERMINISTIC
BEGIN
    DECLARE V_RETURN VARCHAR(255);
    DECLARE V_BOOL INT DEFAULT 0;
          DECLARE FIRST_VARCHAR VARCHAR(1);

    SET FIRST_VARCHAR = left(CONVERT(P_NAME USING gbk),1);
    SELECT FIRST_VARCHAR REGEXP '[a-zA-Z]' INTO V_BOOL;
    IF V_BOOL = 1 THEN
      SET V_RETURN = FIRST_VARCHAR;
    ELSE
      SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT(P_NAME USING gbk),1)),16,10),
          0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
          0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,
          0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
      'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
    END IF;
    RETURN V_RETURN;
END

001.jpg

英文的是小写首字符,汉字的是大写首字母,

select id,name,UPPER(fristPinyin(name)) as first_char from shwcms_album order by id asc limit 10;

统一换成大写

测试完成后开始给数据创建首字母字段保存,并索引.

添加 first_char字段列

UPDATE shwcms_album set first_char = UPPER(fristPinyin(name))

002.jpg