mysql那些事儿之(七)深入select查询
相关链接:
mysql那些事儿之(一)mysql的安装
http:///database/201210/162314.html;
mysql那些事儿之(二)有关数据库的操作
http:///database/201210/162315.html;
mysql那些事儿之(三)有关数据表的操作
http:///database/201210/162316.html;
mysql那些事儿之(四)数据表数据查询操作
http:///database/201210/162317.html;
mysql那些事儿之(五)操作时间
http:///database/201210/162318.html;
mysql那些事儿之(六)字符串模式匹配
http:///database/201210/163969.html
一、表的别名
select * from pet p order by p.age;
这是让查询出的数据按表的年龄排序。
如果在一张宠物表里,想让宠物配对,可以这样利用表的别名:
select p1.name,p1.sex,p2,name,p2.sex from pet as p1,pet as p2 where p1.species = p2.species and p1.sex=m and p2.sex=f;
这个语句的意思是按 物种配对,性别是m和f;
二、列的别名
查询出宠物的名字和种类(直接引用列名)。
select name,species from pet order by name,species;
相似的:
select name,species from pet order by 1,2;
这个和上述的功能是一样的。
还可以为列命名。
select name as n,species as s from pet order by n,s;
这个功能和上述语句是一样的。
在子句中使用列的别名:
select species,count(*) as total from pet group by species having total >1;
bitscn.com
