# 分页
# limit start ,count
# 查询前5个数据
# 查询id 6-10(包含)的 顺序
# 每页显示两个,
# 查询只显示前两个
select * from students where gender=1 limit 2;
# 截止和结束
# select * from students where gender=1 查询到的数据 从第3个开始显示,显示5个
select * from students where gender=1 limit 2,5
# 在sql语句里面 limit放到最后
# 查询数据表中所有女性的信息,并按照身高从高到低排序,只显示两个
select * from students where gender=2 order by height desc limit 0,2;
# 链接查询
# 用于两个表
# 内连接查询 查询的结果为两个表匹配的数据。
# 右连接查询 :查询的结果为两个表匹配的数据,右表特有的数据,对于左表不存在数据使用null填充。
# 左连接查询 :查询的结果为两个表匹配的数据,左表特有的数据,对于右表不存在的数据用null填充。
# 语法
# select * from 表1 inner或left或right join 表2 on 表1.列=表2.列
select * from students inner join classes;
select * from students inner join students.cls_id=classes.id;
# 显示结果
# 1 小明 15 180.0 男 1 1 一班
# 2 小红 16 165.0 男 3 3 三班
# 显示students表格全部,classes表格只显示班级
select students.*,classes.name from students inner join students.cls_id=classes.id;
# 给数据表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;
# 查询 有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称
select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;
# 在以上的查询中,将班级信息放到第一列
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;
# 查询 有能够对应班级的学生以及班级信息,按照班级进行排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;
# 查询 有能够对应班级的学生以及班级信息,按照班级进行排序,班级一样按照id排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id desc;
# left join
# 查询每位学生对应的班级信息
select * from students as s left join classes as c on s.cls_id=c.id;
# 显示结果 左边表格全部显示,右边表格对应不上显示null
# 1 小明 15 180.0 男 1 1 一班
# 2 小红 16 165.0 男 3 3 三班
# 3 小蓝 16 168.0 男 5 null null
select * from students as s left join classes as c on s.cls_id=c.id having c.id=null;
# 显示结果为
# 3 小蓝 16 168.0 男 5 null null
# right join 和 left join取反
# 自关联
# 表格示例
# id atitle p_id
# 1 北京 null
# 2 上海 null
# 3 朝阳区 1
# 4 海淀区 1
# 5 浦东新区 2
# 6 西城区 1
# 查找表格里面北京的区
select * from areas where p_id=1;
# 自关联查询
select * from arear as province inner join areas as city on city.p_id=province.p_id having province.atitle="山东省";
# 只显示省份和城市名
select province.atitle,city.atitle from area as province inner join areas as city on city.pid=province.p_id having province.atitle="山东省";
# 子查询
# 查询最高的男生信息
select * from students where height = (select max(height) from students);
# 列级子查询
# 查询学生的班级号能够对应的学生信息
select * from students where cls_id in (select id from classes);
标签:join,students,链接,classes,Mysql,查询,id,select From: https://blog.51cto.com/u_15828115/5754006