找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
对表数据进行增删改查
查询select
1,查看一个表的所有数据
select * from +表名
2,查看表中指定的字段的数据
例,查看student表中指定字段id  ,name   class  的数据
   select id,name,class from student

C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/536e5eb24b9248e6835939e3b7e9f370/dxmvo%7E%28_vq@%60v%28z8r0t%24%60k7.png

3,给表中的字段取别名  as
select name as “姓名”,class as“班级” from student

4,在表中查询指定条件的数据;也就是我们进行条件筛选
select * from +表名 where +条件;

例子;在学生表中查询所有女同学的数据
select * from student where sex=“女”;


where条件使用
where +条件  =、 >、<、!= 、>= 、<=

where +多个条件
and 、or 、 in、 is null 、 is not null 、 between   and

and 且: 条件1  and  条件2  and 条件3
3个条件需要同时满足

or  或    条件1  or  条件2  or 条件3
3个条件至少满足一个

in (22,23,26)  在这3个条件之中

between   and 在这个范围之间      (范围只能从小到大不能从大到小)
C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/46240adf4229407e9abf579c46234141/mvhg%60yyjyf%7Dkbz%60%24g9h7vv7.png
判断一个是否为空
求学生表中没有参加English考试的学生?
English=null  这个写法是错误的

只能用English is null  这样写
C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/3cc6fc17134e461dacd8eb99f0bc58ae/%255_h%24iqw7zdl@3%7D3nqk%7E6j8.png

查询数据不为空    is  not  null

[img=620,0]C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/d8435358ee0c4a78ba002dd94ec0f0fd/e1c%7Ep65ohlt%24t%7Dg6%605@nemy.png[/img]


注意点:  在数据库中,null只是一个状态,不是一个具体的值


5,对表的数据进行排序  order by
asc :默认的升序,从小到大
desc:默认的降序,从大到小
select * from +表名 order by +字段名 asc
例子:根据年龄进行升序
select * from student order by age asc

注意点
1;如果是默认的升序排列,那么asc可以省略不写
order by 默认是升序
2,order by 后面最多只能接2个条件的序,以第一个为主
例子:班级先进行升序,再根据年龄进行降序
select * from student order by class,age desc

6,模糊匹配 where条件  like 类似
%:表示匹配0个或多个
_:表示匹配一个字符
select * from +表名 where +字段名 like + ‘匹配的条件’;
例子匹配以3开头的所有年龄
select * from student where age like ‘3_’;
匹配班级包含19数字的所有班级
select * from student where class like ‘%19%’;
匹配姓名以san结尾的所有信息
select * from student where name like‘%san’;
C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/139b631d97d744cf9b2f8faf8353643d/k%28%5B%25%60%60%6003xe74s3%5Bo37o%5D%280.png

C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/2442deead6c4461fb6c655e1cfb218a4/60%5D0ziluwd2n2@%60am%7D%292b6g.png


7,限制  limit  m,n  后面加2个数值
第一个值m:是从第几行开始,索引值(从零开始)(为0时是指从第一行开始)
也就是说我们第一行数据对应的索引值是0
第二个值n;表示显示多少行数据
例子:limit 0,4
select * from student limit 0,4;
C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/69b72a72e3e84e0bbc9a48a4afc687bb/%24152g%5Bxc%5Bkz%60%25cd%5Dto%7Dzbns.png
查看学生表中2到4行的数据
select * from student limit 1,3;


统计年龄最大的前3行数据
select * from zuoye order by age desc limit 0,3;

8,数据库中聚合函数
max 最大
min 最小
avg 平均值
sum 求和
count 总数(用来看数据有多少条)
distinct 去重复

求学生表中年纪最大的是多少岁?
select max(age) from zuoye;
求学生表中年纪最小的是多少岁?
select min(age) from zuoye;
求学生表中平均年龄?
select avg(age) from zuoye;
求学生表中年龄的求和?
select sum(age) from zuoye;
查看学生表中有哪些班级
select distinct(class) from zuoye

select count(1) from zyoye  这个可以用来查看数据条数

求学生表中年龄最大的学生所有信息

select * from zuoye order by age desc limit 0,1;

select id,name,class, max(age),sex,chinese,math,english,number from zuoye;    信息不对

SQL嵌套或者子查询
第一步:求出最大学生年龄
select max(age) from zuoye   实际结果是34
第二步,求年纪是34的学生的信息
select *from zuoye where age=34

select * from zuoye where age=(select max(age) from zuoye);


=后面使用()
子sql结果是一个值才能使用=
子sql结果是多个值,需要使用in



10,分组  group by 一般分组不会单独使用,通常是结合我们的聚合函数组合使用
例子:男女分组,把男的,女的最大年龄显示出来
select sex,max(age) from zuoye group by sex

C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/475a698de86b47a58a23b92aac157654/quk%24asf%243ij@%24d%7D9nkv%254%29o.png


注意点;聚合函数只能和group by 后面的字段名同时查询,其他的字段不能和聚合函数同时查询,如果查询的字段中有聚合函数,除了聚合函数其他查询的字段都是取满足条件的第一条数据

11,having +条件 和 where +条件用法一样,但是使用场景不同,一般group by后再加条件时,只能用having不能用where
例子:球学生表中班级总成绩大于400分的班级

select class,sum(chinese+math+english) sum from zuoye group by class having sum>400;

在sum(chinese+math+english) sum 表示把sum(chinese+math+english)起名叫sum
C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/3a6c9eefaaf4476ba39065d4573a746b/qg6%29%60m5%24al%7D%29az00v6a8%24lo.png

C:/Users/Administrator/AppData/Local/YNote/data/qq788A1E66A631CF4E5CC90A0BEBD9F713/d6d298faaaaa4d89959b2e229963cb52/clipboard.png
注意点,
1,group by 后面使用条件判断,只能用having不能用
2,having后面条件只能是聚合函数或者是根据什么分组的字段
3,一般都是聚合函数和分组以及having组合使用


分享至 : QQ空间
收藏

0 个回复

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