当前位置:Gxlcms > 数据库问题 > MySQL学习之路(五)MySQL高级查询

MySQL学习之路(五)MySQL高级查询

时间:2021-07-01 10:21:17 帮助过:6人阅读

  count():统计数量;

  max():统计最大值;

  min():统计最小值;

  avg():统计平均数;

  sum():统计和;

Select count(*) from student;

MySQL排序  group by; order by;

  默认是升序排序;

Select * from student group by sid asc;--升序排序
Select * from student group by sid DESC;--降序排序
Select * from student group by sid,sname;---多字段排序

having使用方法和where相同,功能比where强大;

limit 限定查询

  limit 查询数据表的指定数量的数据

  limit 只能限定查询

  主要用来实现分页效果;

Select * from student limit 1;
--查询1条数据
Select * from student limit 1,2;
--从下标1开始查询两条数据;

MySQL连接查询

  交叉连接:cross join;从一张表中循环取出每一条记录,每一条记录都会跟另一张表进行连接匹配;

      连接方式: 左表 cross join 右表;

  内连接:左表 inner join 右表 on 条件;

  别名:as(可以省去as)

  外链接:outer join;

  外左连接;left join;

  外右连接:right join;

Select * from student cross join student2;
Select * from student inner join student2 on student.sid = student2.sid;
Select * from student as a inner join student2 b on a.sid = b.sid;
Select * from student as a left outer join student2 b on a.sid = b.sid;
Select * from student as a right outer join student2 b on a.sid = b.sid;

多表查询  union(all;distinct)

  all:不去重;保留所有数据;

  distinct:去重 (默认);

 

MySQL学习之路(五)MySQL高级查询

标签:min   nio   avg   weight   max   bsp   方法   数据表   字段排序   

人气教程排行