数据库select的操作与理解

本文详细介绍SQL查询的基础操作及高级技巧,包括基本查询、条件筛选、排序、聚合函数等,并通过实例演示如何高效地进行数据检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于数据库而言除了插入,删除,用的最多的还是查询,一般情况下是不会对数据库做过多的改动,因此查询用到的最广泛。

查询主要是对于表中的信息来说进行查询,查询的方法有很多种,下面的操作和理解也仅是我自己的理解~

select


先创建一张表然后插入一些数据以便进行下面的操作:

create table student(id int not null default 1, 
name varchar(20) not null default '', 
chinese float not null default 0.0 comment ' 语文成绩', 
english float not null default 0.0 comment '英语成绩',
 math float not null default 0.0 comment '数学成绩' );
insert into student values(1,'Jack',89,78,90);

insert into student values(2, 'Mary', 67,98, 56);

insert into student values(3, 'Rose', 87,78, 77);

insert into student values(4, 'Alice', 88,98, 90);

insert into student values(5, 'Bob', 82,84, 67);

指定列查询

select id,name,math from student;

去掉查询列中的重复行,关键字distinct

select distinct math from student;

使用as给列其别名,避免列名太长。

select name,chinese+math+english+10 as total_grade from student;

使用like进行模糊查询,可与%配套使用

select name,chinese+math+english+10 as total_grade from student where name like 'J%';

where子句


查询R开头的学生的成绩

select * from student where name like 'R%';

查询英语成绩大于90的学生的所有成绩

select * from student where english>90;

并列查询,两个条件一起查询,用and关键字并列。

select * from student where english >60 and id>2;

区间查询,between .....and....

select * from student where english between 80 and 90;

查询指定分数的学生信息

select * from student where math in(89,99,50);

order by子句


order by 子句应该位于select语句的后面,升序是asc,降序是desc,默认是升序排列。

将数学成绩和英语成绩的和降序排列

select id,name ,math+english as total from student order by total desc;

count,统计某一行或某一列的总数

统计总共有多少学生

select count(*) from student;

统计有多少学生数学成绩是大于等于90分

select count(*) from student where math>=90;

sum 返回一行或一列的总和,只对数值有效。

查询所有同学数学成绩的总和

select sum(math) from student;

avg 返回一行或一列的平均值

查询所有同学的数学平均分

select avg(math) from student;

max,返回一列值的最大值

min,返回一列值的最小值

group by子句对列进行分组,为此另外建表演示。

查询每个部门的平均工资和最高工资,group by说明是以deptno分组进行排列。

select deptno,avg(sal),max(sal) from emp group by deptno;

查询每个部门的每个岗位的平均工资和最低工资,以deptno和job分组,表示了先以deptno分组,然后再在部门里以job 分组。

select avg(sal),min(sal),job,deptno from emp group by deptno,job;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值