找回密码
 立即注册
  • 便民服务
  • 关注我们
  • 社区新手
创建表格:create table student(id int(20) primary key,age int(8),sex int(4),name varchar(20),class int(4),math int(4));

1、select * from XX(表名):查看xx表所有字段的数据
2、slelect name from xx(表名):查询xx表所有的name
3、select name,math from xx(表名):查询xx表name和math字段对应的数据
4、select * from xx where class=1833:where后面接条件,查询满足某个条件的数据
查询班级为1833班的所有信息
注意错误示范!!!:
select * from xx(表student) where class=1833 and class=1834;
在上面语句中,and左右不能接同一个字段等于多个值,只能用下面or进行连接
正确的:elect * from xx(表student) where class=1833 or class=1834;查询班级为1833班和1834班的信息
5、select * from xx(表student) where class=1833 and class=1834;(and左右接不同字段等于具体值就可以)查询查询班级为1833和姓名为zhangsan的信息
6、select * from xx(表student) where class !=1833;(!=表示不等于)查询班级不为1833班的信息
select * from xx(表student) where class <>1833;查询班级不为1833班的信息
7、select * from xx(表student) where age between 22 and 27;查询年龄22到27之间的学生信息包含22和27
8、select * from xx(表student) where age>=22 and age<=27;查询年龄大于等于22和小于等于27的学生信息其作用等同于 between and
9、select * from xx(表student) where age>22 and age<27;查询年龄大于22和小于27的学生信息不包含22和27
10、注意错误示范!!!:select * from xx(表student) where class =‘null’;查询班级为空不是等于空值
正确的:select * from xx(表student) where class is null;查询班级为空的学生信息,这里的空是空属性
11、select * from xx(表student) where class is not null;查询班级不为空的信息
12、select * from xx(表student) where nane like ‘%ao%’;查询姓名中间包含ao的学生信息
13、select * from xx(表student) where id limit 3,5;查询4到8行的数据,3指的是第四行的下标为3,5指的是取多少行,就是说4到8行有多少行
14、select * from  xx(表student)where id limit 8;limit 后面直接接一个数字,表示取前几行,这里取前8行数据
15、select * from xx(表student) order by age asc;将年龄按照从小到大的顺序排列,asc:升序
desc:降序
从小到大:asc(字母少的代表小到大)
大到小desc(字母多的代表大到小)
16、select * from xx(表student) order by age desc;将年龄按照大到小的顺序排列
17、select count(class) from  xx(表student);统计class字段的行数,其中函数有为空的,不会被统计
select count(*) from  xx(表student);(*表示所有字段)统计student表总共有多少行
18、select sum(math) from  xx(表student);查询表中数学成绩总和
select avg(math) from  xx(表student);查询平均数学成绩
select max(math) from  xx(表student);查询最大的数学成绩是多少
select min(math) from  xx(表student);查询最小的数学成绩是多少
select distinct(sex) from  xx(表student);对sex字段进行去重

总结;
统计查询数据的数量
select count(*)from +表名
查询某个字段求和
select sum(字段)from +表名
查询某个字段进行平均值
select avg(字段)from +表名
查询某个字段最大值
select max(字段)from +表名
查询某个字段最小值
select min(字段)from +表名
对某个字段进行去重
select distinct(字段)from +表名









单表总结:
1、where不能放在group by 后面
where子句的作用是对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数比如sum(),avg()等,使用where
条件显示特定的行。
2、having是跟group by连在一起用的,放在group by后面,此时的作用相当于where。
hacing子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having条件显示特定的组,也可以使用多个分组标准进行分组。

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册