
<?php
function Pinyin($_String, $_Code='gb2312')
{
$_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha".
"|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|".
"cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er".
"|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui".
"|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang".
"|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang".
"|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue".
"|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne".
"|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen".
"|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang".
"|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|".
"she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|".
"tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu".
"|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you".
"|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|".
"zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo"
$_DataValue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990".
"|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725".
"|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263".
"|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003".
"|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697".
"|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211".
"|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922".
"|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468".
"|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664".
"|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407".
"|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959".
"|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652".
"|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369".
"|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128".
"|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914".
"|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645".
"|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149".
"|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087".
"|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658".
"|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340".
"|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888".
"|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585".
"|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847".
"|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055".
"|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780".
"|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274".
"|-10270|-10262|-10260|-10256|-10254"
$_TDataKey = explode('|', $_DataKey)
$_TDataValue = explode('|', $_DataValue)
$_Data = (PHP_VERSION>='5.0') ? array_combine($_TDataKey, $_TDataValue) : _Array_Combine($_TDataKey, $_TDataValue)
arsort($_Data)
reset($_Data)
if($_Code != 'gb2312') $_String = _U2_Utf8_Gb($_String)
$_Res = ''
for($i=0$i<strlen($_String)$i++)
{
$_P = ord(substr($_String, $i, 1))
if($_P>160) { $_Q = ord(substr($_String, ++$i, 1))$_P = $_P*256 + $_Q - 65536}
$_Res .= _Pinyin($_P, $_Data)
}
return preg_replace("/[^a-z0-9]*/", '', $_Res)
}
function _Pinyin($_Num, $_Data)
{
if ($_Num>0 &&$_Num<160 ) return chr($_Num)
elseif($_Num<-20319 || $_Num>-10247) return ''
else {
foreach($_Data as $k=>$v){ if($v<=$_Num) break}
return $k
}
}
function _U2_Utf8_Gb($_C)
{
$_String = ''
if($_C <0x80) $_String .= $_C
elseif($_C <0x800)
{
$_String .= chr(0xC0 | $_C>>6)
$_String .= chr(0x80 | $_C &0x3F)
}elseif($_C <0x10000){
$_String .= chr(0xE0 | $_C>>12)
$_String .= chr(0x80 | $_C>>6 &0x3F)
$_String .= chr(0x80 | $_C &0x3F)
} elseif($_C <0x200000) {
$_String .= chr(0xF0 | $_C>>18)
$_String .= chr(0x80 | $_C>>12 &0x3F)
$_String .= chr(0x80 | $_C>>6 &0x3F)
$_String .= chr(0x80 | $_C &0x3F)
}
return iconv('UTF-8', 'GB2312', $_String)
}
function _Array_Combine($_Arr1, $_Arr2)
{
for($i=0$i<count($_Arr1)$i++) $_Res[$_Arr1[$i]] = $_Arr2[$i]
return $_Res
}
//用法:
//第二个参数留空则为gb1232编码
echo Pinyin('中国站长天空')
//第二个参数随意设置则为utf-8编码
echo Pinyin('中国站长天空',1)
?>
一、mysql查询的五种子句where(条件查询)、having(筛选)、group by(分组)、order by(排序)、limit(限制结果数)
1、where常用运算符:
比较运算符
>, <,= , != (<>),>= , <=
in(v1,v2..vn)
between v1 and v2在v1至v2之间(包含v1,v2)
逻辑运算符
not ( ! ) 逻辑非
or ( || )逻辑或
and ( &&) 逻辑与
where price>=3000 and price <= 5000 or price >=500 and price <=1000
取500-1000或者3000-5000的值
where price not between 3000 and 5000
不在3000与5000之间的值
模糊查询
like 像
通配符:
% 任意字符
_ 单个字符
where goods_name like '诺基亚%'
where goods_name like '诺基亚N__'
2、group by 分组
一般情况下group需与统计函数(聚合函数)一起使用才有意义
如:select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id
这里取出来的结果中的good_name是错误的!因为shop_price使用了max函数,那么它是取最大的,而语句中使用了group by 分组,那么goods_name并没有使用聚合函数,它只是cat_id下的第一个商品,并不会因为shop_price改变而改变
mysql中的五种统计函数:
(1)max:求最大值
select max(goods_price) from goods
这里会取出最大的价格的值,只有值
#查询每个栏目下价格最高的
select cat_id,max(goods_price) from goos group by cat_id
#查出价格最高的商品编号
select goods_id,max(goods_price) from goods group by goods_id
(2)min:求最小值
(3)sum:求总数和
#求商品库存总和
select sum(goods_number) from goods
(4)avg:求平均值
#求每个栏目的商品平均价格
select cat_id,avg(goods_price) from goods group by cat_id
(5)count:求总行数
#求每个栏目下商品种类
select cat_id,count(*) from goods group by cat_id
###要把每个字段名当成变量来理解,它可以进行运算###
例:查询本店每个商品价格比市场价低多少;
select goods_id,goods_name,goods_price-market_price from goods
查询每个栏目下面积压的货款
select cat_id,sum(goods_price*goods_number) from goods group by cat_id
###可以用as来给计算结果取个别名###
select cat_id,sum(goods_price * goods_number) as hk from goods group by cat_id
不仅列名可以取别名,表单也可以取别名
3、having 与where 的异同点
having与where类似,可以筛选数据,where后的表达式怎么写,having后就怎么写
where针对表中的列发挥作用,查询数据
having对查询结果中的列发挥作用,筛选数据
#查询本店商品价格比市场价低多少钱,输出低200元以上的商品
select goods_id,good_name,market_price - shop_price as s from goods having s>200
//这里不能用where因为s是查询结果,而where只能对表中的字段名筛选
如果用where的话则是:
select goods_id,goods_name from goods where market_price - shop_price >200
#同时使用where与having
select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s >200
#查询积压货款超过2万元的栏目,以及该栏目积压的货款
select cat_id,sum(shop_price * goods_number) as t from goods group by cat_id having s >20000
#查询两门及两门以上科目不及格的学生的平均分
思路:
#先计算所有学生的平均分
select name,avg(score) as pj from stu group by name
#查出所有学生的挂科情况
select name,score<60 from stu
#这里score<60是判断语句,所以结果为真或假,mysql中真为1假为0
#查出两门及两门以上不及格的学生
select name,sum(score<60) as gk from stu group by name having gk >1
#综合结果
select name,sum(score<60) as gk,avg(score) as pj from stu group by name having gk >1
4、order by
(1) order by price //默认升序排列
(2)order by price desc //降序排列
(3)order by price asc //升序排列,与默认一样
(4)order by rand() //随机排列,效率不高
#按栏目号升序排列,每个栏目下的商品价格降序排列
select * from goods where cat_id !=2 order by cat_id,price desc
5、limit
limit [offset,] N
offset 偏移量,可选,不写则相当于limit 0,N
N 取出条目
#取价格第4-6高的商品
select good_id,goods_name,goods_price from goods order by good_price desc limit 3,3
###查询每个栏目下最贵的商品
思路:
#先对每个栏目下的商品价格排序
select cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price desc
#上面的查询结果中每个栏目的第一行的商品就是最贵的商品
#把上面的查询结果理解为一个临时表[存在于内存中]【子查询】
#再从临时表中选出每个栏目最贵的商品
select * from (select goods_id,goods_name,cat_id,shop_price from goods order by cat_id,shop_price desc) as t group by cat_id
#这里使用group by cat_id是因为临时表中每个栏目的第一个商品就是最贵的商品,而group by前面没有使用聚合函数,所以默认就取每个分组的第一行数据,这里以cat_id分组
良好的理解模型:
1、where后面的表达式,把表达式放在每一行中,看是否成立
2、字段(列),理解为变量,可以进行运算(算术运算和逻辑运算)
3、 取出结果可以理解成一张临时表
二、mysql子查询
1、where型子查询
(把内层查询结果当作外层查询的比较条件)
#不用order by 来查询最新的商品
select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods)
#取出每个栏目下最新的产品(goods_id唯一)
select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id)
2、from型子查询
(把内层的查询结果供外层再次查询)
#用子查询查出挂科两门及以上的同学的平均成绩
思路:
#先查出哪些同学挂科两门以上
select name,count(*) as gk from stu where score <60 having gk >=2
#以上查询结果,我们只要名字就可以了,所以再取一次名字
select name from (select name,count(*) as gk from stu having gk >=2) as t
#找出这些同学了,那么再计算他们的平均分
select name,avg(score) from stu where name in (select name from (select name,count(*) as gk from stu having gk >=2) as t) group by name
3、exists型子查询
(把外层查询结果拿到内层,看内层的查询是否成立)
#查询哪些栏目下有商品,栏目表category,商品表goods
select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id)
三、union的用法
(把两次或多次的查询结果合并起来,要求查询的列数一致,推荐查询的对应的列类型一致,可以查询多张表,多次查询语句时如果列名不一样,则取第一次的列名!如果不同的语句中取出的行的每个列的值都一样,那么结果将自动会去重复,如果不想去重复则要加all来声明,即union all)
## 现有表a如下
id num
a5
b10
c15
d10
表b如下
id num
b5
c10
d20
e99
求两个表中id相同的和
select id,sum(num) from (select * from ta union select * from tb) as tmp group by id
//以上查询结果在本例中的确能正确输出结果,但是,如果把tb中的b的值改为10以查询结果的b的值就是10了,因为ta中的b也是10,所以union后会被过滤掉一个重复的结果,这时就要用union all
select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id
#取第4、5栏目的商品,按栏目升序排列,每个栏目的商品价格降序排列,用union完成
select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 union select goods_id,goods_name,cat_id,shop_price from goods where cat_id=5 order by cat_id,shop_price desc
【如果子句中有order by 需要用( ) 包起来,但是推荐在最后使用order by,即对最终合并后的结果来排序】
#取第3、4个栏目,每个栏目价格最高的前3个商品,结果按价格降序排列
(select goods_id,goods_name,cat_id,shop_price from goods where cat_id=3 order by shop_price desc limit 3) union (select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 order by shop_price desc limit 3) order by shop_price desc
四、左连接,右连接,内连接
现有表a有10条数据,表b有8条数据,那么表a与表b的笛尔卡积是多少?
select * from ta,tb //输出结果为8*10=80条
1、左连接
以左表为准,去右表找数据,如果没有匹配的数据,则以null补空位,所以输出结果数>=左表原数据数
语法:select n1,n2,n3 from ta left join tb on ta.n1= ta.n2 [这里on后面的表达式,不一定为=,也可以>,<等算术、逻辑运算符]【连接完成后,可以当成一张新表来看待,运用where等查询】
#取出价格最高的五个商品,并显示商品的分类名称
select goods_id,goods_name,goods.cat_id,cat_name,shop_price from goods left join category on goods.cat_id = category.cat_id order by shop_price desc limit 5
2、右连接
a left join b 等价于 b right join a
推荐使用左连接代替右连接
语法:select n1,n2,n3 from ta right join tb on ta.n1= ta.n2
3、内连接
查询结果是左右连接的交集,【即左右连接的结果去除null项后的并集(去除了重复项)】
mysql目前还不支持 外连接(即左右连接结果的并集,不去除null项)
语法:select n1,n2,n3 from ta inner join tb on ta.n1= ta.n2
总结:可以对同一张表连接多次,以分别取多次数据
应该需要把汉字对应的拼音先存在数据表,然后查询吧;查询语句是:select 数据库中存储的中文名字 from 名字表 where 数据库中英文名 like 输入的英文;
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)