mysql的基本查询语句select例子

普通查询

>>select * from stu;

例1:查询表名为stu 的所有信息

>>select id,name,age from stu;

例2:查询stu表中的id,name,age的数据

>>select id,name,age from stu where age>20;

例3:查询age>20的数据的id,name,age

去重查询

>>select distinst age from stu;

例:查询stu 表中所有数据,当age的数据一样时只显示一条

排序查询

>>order by age (asc/desc)

例:将age按升序/降序排,不标明默认为升序

等值查询

>>select name,score from stu,result where stu.id=result.id and age>=20 and score<60;

例:查询stu表和result表中age>=20和score<60的数据

左外链接查询

>>select name,score where from(select id,name from stu where age>=20)a

>>left join(select id,score from reselt where score<60)b

>>on a.id=b.id;

例:查询a表中的age>=20的id和name,和查询b表中score<60的id和name

(意思是a表中储存id name,b表中储存id score,age>=20并且score<60的数据与a表的id和b表的id相匹配,输出name和score

右外连接查询

>>把左外连接查询的right换成left即可

(两者不同的是 左外连接查询需要左表的数据全部在,右外连接查询需要右表的数据全部在,

左外连接查询和右外连接查询都能有效地提高查询效率)

全外连接查询

内连接查询

>>把左外连接查询的right换成inner即可

(要求所查询的数据都无null存在)

联合查询

>>select id,name,age,sex from teacher union select id,name,age,sex from stu;

例:联合查询teacher表和stu表中id,name,age,sex

(要求对应的类型相同,union自带去重功能,不想去重加 union all)

猜你喜欢

转载自blog.csdn.net/obitosbb/article/details/90181523