用MySQL数据库来处理中英文取首字母排序

我们的联系人列表经常会有按照首字母排序的需求. 这个很方便, 很清晰, 如果不说性能如何, 就像让数据库来做呢? 其实MySQL是支持的.

我们建立一个函数:

DELIMITER $$
CREATE FUNCTION `fristPinyin`(P_NAME VARCHAR(255)) 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$$
DELIMITER;

然后我们执行SQL

select intermediary_id, intermediary_name, fristPinyin(intermediary_name) as fristPinyin
from t_second_agent_intermediary
where city_id = 1337  and intermediary_type = 'zj'
ORDER BY fristPinyin ;

或者简单点(改成你自己的表或者字段就成)

select name,fristPinyin(name) as fristPinyin from users
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页