mysql查询

数据表:学生表(student)和学生分数表(score)
学生表成绩表

一.查询 不重复记录 distinct
语法:select distinct field from tablename [where condition];

1.1.查询学生表(student),获取所有的班级(去重)。

sql:select distinct class from student;

班级信息
1.2.这个distinct可以配合聚合函数使用,获取班级数量(去重)。

sql:select count(distinct class) from student;

配合聚合函数

二.排序 order by
语法:select * from tablename order by [field1 [asc/desc],field2 [asc/desc],...fieldn [asc/desc]];

2.1查询分数表(score) ,首先用总分(fraction)排,如果总分相同,再按照最高分(highest)排,都按从大到小排序

sql:select * from score order by fraction desc,highest desc;

在这里插入图片描述

三.获取部分数据 limit
语法:select * from tablename [limit offset_start,row_count];

3.1 查询分数表(score),获取数据表中第六条至第十条的记录。

sql:select * from score limit 5,5;

查询分数

四.聚合函数
语法:select * fun_name from tablename from 
       [where condition]
       [group by field1,field2,...fieldn]
       [with rollup]
       [having condition];

4.1.统计学生表(student)中的总人数 count

sql:select count(*) from student;

统计的总人数
4.2.统计学生表(student)每个班级的人数 count ,group by

sql:select class,count(class) from student group by class;

班级人数
4.3.更详细一点,统计学生表(student)每个班级的人数,并且统计总人数 count,group by,with rollup

sql:select class,count(class) from student group by class with rollup;

统计总人数
4.4.统计学生表(student)每个班级人数大于二的班级 count,group by,having

sql:select class,count(class) from student group by class having count(class) >2;

人数大于二的班级
剩下几个聚合函数就不一一写了。

五.表连接
语法:
   内连接: ... from tab1 inner join  tab2 on condition ...
   外连接:
     左连接:... from tab1 left  join tab2 on condition ...
     右连接:... from tab1 right join tab2 on condition ...``
正在更新…
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值