按汉语拼音首字母查询数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按汉语拼音首字母查询数据相关的知识,希望对你有一定的参考价值。
页面传入字母。然后在数据库查询出汉字拼音开头字母的数据,(急)在线等。。。。。。要源码注:用java编程!~
参考技术A 方法一:建一个拼音表 t_cosler ,存放每个字母开头的第一个汉字的编号和最后一个汉字的编号。 +------+--------+-------+ | f_PY | cBegin | cEnd | +------+--------+-------+ | A | 45217 | 45252 | | B | 45253 | 45760 | ..... | Z | 54481 | 55289 | +------+--------+-------+ 然后直接查询就行了。mysql> create table t_cosler( -> f_PY char primary key, -> cBegin SMALLINT UNSIGNED not null, -> cEnd SMALLINT UNSIGNED not null -> ); Query OK, 0 rows affected (0.09 sec) mysql> insert into t_cosler values -> ('A',0xB0A1,0xB0C4), -> ('B',0xB0C5,0xB2C0), -> ('C',0xB2C1,0xB4ED), -> ('D',0xB4EE,0xB6E9), -> ('E',0xB6EA,0xB7A1), -> ('F',0xB7A2,0xB8C0), -> ('G',0xB8C1,0xB9FD), -> ('H',0xB9FE,0xBBF6), -> ('J',0xBBF7,0xBFA5), -> ('K',0xBFA6,0xC0AB), -> ('L',0xC0AC,0xC2E7), -> ('M',0xC2E8,0xC4C2), -> ('N',0xC4C3,0xC5B5), -> ('O',0xC5B6,0xC5BD), -> ('P',0xC5BE,0xC6D9), -> ('Q',0xC6DA,0xC8BA), -> ('R',0xC8BB,0xC8F5), -> ('S',0xC8F6,0xCBF9), -> ('T',0xCBFA,0xCDD9), -> ('W',0xCDDA,0xCEF3), -> ('X',0xCEF4,0xD188), -> ('Y',0xD1B9,0xD4D0), -> ('Z',0xD4D1,0xD7F9); Query OK, 23 rows affected (0.16 sec) Records: 23 Duplicates: 0 Warnings: 0 mysql> select * from o_personnel; +------+------------+ | A_Id | A_UserName | +------+------------+ | 1 | 首先 | | 2 | 检查 | | 3 | 我们 | | 4 | 的二 | | 5 | 进制 | | 6 | 是否 | | 7 | 适合 | | 8 | 你的 | | 9 | 平台 | +------+------------+ 9 rows in set (0.00 sec) mysql> select p.*,c.* -> from o_personnel p , t_cosler c -> where CONV(HEX(left(A_UserName,1)),16,10) between c.cBegin and c.cEnd; +------+------------+------+--------+-------+ | A_Id | A_UserName | f_PY | cBegin | cEnd | +------+------------+------+--------+-------+ | 4 | 的二 | D | 46318 | 46825 | | 2 | 检查 | J | 48119 | 49061 | | 5 | 进制 | J | 48119 | 49061 | | 8 | 你的 | N | 50371 | 50613 | | 9 | 平台 | P | 50622 | 50905 | | 1 | 首先 | S | 51446 | 52217 | | 6 | 是否 | S | 51446 | 52217 | | 7 | 适合 | S | 51446 | 52217 | | 3 | 我们 | W | 52698 | 52979 | +------+------------+------+--------+-------+ 9 rows in set (0.00 sec) mysql> 查S开头的
mysql> select p.* -> from o_personnel p , t_cosler c -> where CONV(HEX(left(A_UserName,1)),16,10) between c.cBegin and c.cEnd -> and c.f_PY='S'; +------+------------+ | A_Id | A_UserName | +------+------------+ | 1 | 首先 | | 6 | 是否 | | 7 | 适合 | +------+------------+ 3 rows in set (0.00 sec) mysql> 方法二:不用这个t_cosler表,直接写个函数权限汉字得到拼音。
局限性: 以上方法,均依照汉字区位表来实现,对区位后面的复杂字,无法准确判断,对多音字无法准确判断。
方法三:从微软拼音中导出所有汉字的拼音表。方法四不建表 mysql> SELECT *, -> ELT(INTERVAL(CONV(HEX(left(A_UserName,1)),16,10), -> 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0 xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCE F4,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') as PY -> from o_personnel; +------+------------+------+ | A_Id | A_UserName | PY | +------+------------+------+ | 1 | 首先 | S | | 2 | 检查 | J | | 3 | 我们 | W | | 4 | 的二 | D | | 5 | 进制 | J | | 6 | 是否 | S | | 7 | 适合 | S | | 8 | 你的 | N | | 9 | 平台 | P | +------+------------+------+ 9 rows in set (0.00 sec) mysql> 方法五: mysql> SELECT *, -> CHAR(INTERVAL(CONV(HEX(left(A_UserName,1)),16,10), -> 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE, -> 0xBBF7,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE, -> 0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCDDA,0xCDDA,0xCEF4, -> 0xD1B9,0xD4D1)+64) as PY -> from o_personnel; +------+------------+------+ | A_Id | A_UserName | PY | +------+------------+------+ | 1 | 首先 | S | | 2 | 检查 | J | | 3 | 我们 | W | | 4 | 的二 | D | | 5 | 进制 | J | | 6 | 是否 | S | | 7 | 适合 | S | | 8 | 你的 | N | | 9 | 平台 | P | +------+------------+------+ 9 rows in set (0.00 sec) mysql> 参考技术B 说下你的QQ 我联系你怎么做 参考技术C 这里有个网页 http://m.cnblogs.com/55979/1648972.html,看看吧,我找了,这里只能用存储过程,你看看,应该可以帮你解决问题。
按拼音首字母排序
<?php
$temp = array();
// 按拼音首字母排序
foreach($res as $key=>$val){
$index = $this->sortByPinyin($val[‘note_name‘]);
$temp[$index][] = $val; //先用中间变量数组,根据字母A B C..装着集合在一起的
}
$list = array();
foreach($temp as $k=>$v){
$list[$k][‘index‘] = $k;
$list[$k][‘list‘] = $v;
}
array_multisort($list); //用 array_multisort按键排序,也可用 ksort()
// ksort($list);
$list = array_values($list); //最后将键弄回数字的那些键 , A, B ,C ... => 0, 1, 2....
/**
* [获取字符串的拼音首字母]
* @param [type] $str [description]
* @return [type] [description]
*/
public function sortByPinyin($str)
{
if(empty($str)){
return ‘‘;
}
$fchar = ord($str{0});
if($fchar>=ord(‘A‘)&&$fchar<=ord(‘z‘)){
return strtoupper($str{0});
}
$s1 = iconv(‘UTF-8‘,‘gb2312‘,$str);
$s2 = iconv(‘gb2312‘,‘UTF-8‘,$s1);
$s = $s2==$str?$s1:$str;
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319&&$asc<=-20284) return ‘A‘;
if($asc>=-20283&&$asc<=-19776) return ‘B‘;
if($asc>=-19775&&$asc<=-19219) return ‘C‘;
if($asc>=-19218&&$asc<=-18711) return ‘D‘;
if($asc>=-18710&&$asc<=-18527) return ‘E‘;
if($asc>=-18526&&$asc<=-18240) return ‘F‘;
if($asc>=-18239&&$asc<=-17923) return ‘G‘;
if($asc>=-17922&&$asc<=-17418) return ‘H‘;
if($asc>=-17417&&$asc<=-16475) return ‘J‘;
if($asc>=-16474&&$asc<=-16213) return ‘K‘;
if($asc>=-16212&&$asc<=-15641) return ‘L‘;
if($asc>=-15640&&$asc<=-15166) return ‘M‘;
if($asc>=-15165&&$asc<=-14923) return ‘N‘;
if($asc>=-14922&&$asc<=-14915) return ‘O‘;
if($asc>=-14914&&$asc<=-14631) return ‘P‘;
if($asc>=-14630&&$asc<=-14150) return ‘Q‘;
if($asc>=-14149&&$asc<=-14091) return ‘R‘;
if($asc>=-14090&&$asc<=-13319) return ‘S‘;
if($asc>=-13318&&$asc<=-12839) return ‘T‘;
if($asc>=-12838&&$asc<=-12557) return ‘W‘;
if($asc>=-12556&&$asc<=-11848) return ‘X‘;
if($asc>=-11847&&$asc<=-11056) return ‘Y‘;
if($asc>=-11055&&$asc<=-10247) return ‘Z‘;
return null;
}
最后的结果是:(下面是json的形式)
[ { "index": "A", "list": [ { "id": "6", "suid": "1", "note_name": "哎去" }, { "id": "7", "suid": "1", "note_name": "abf" } ] }, { "index": "B", "list": [ { "id": "9", "suid": "1", "note_name": "标签2" } ] }, { "index": "C", "list": [ { "id": "1", "suid": "1", "note_name": "测试" } ] }, { "index": "D", "list": [ { "id": "3", "suid": "1", "note_name": "DSA" } ] } ]
以上是关于按汉语拼音首字母查询数据的主要内容,如果未能解决你的问题,请参考以下文章