数据库基本操作

本文详细介绍了使用SQL进行数据查询的各种技巧,包括单表查询、多表联查、条件筛选等核心操作,并提供了具体示例帮助理解。
一 单表:
student
idnamegradegender
1张三50
2李四40
3王五100
4赵六89
5孙丽66
6王田77
7李可97
8杨庄nullnull
 
1. 查询性别(展示不同的性别)
   select distinct(gender) from student
2.查询大于60分,小于90分学生的信息
  select * from student where grade<=90 and grade>=60
  select * from student where grade between 60 and 90
3.查看性别为空的学生信息
  select * from student where gender is null
4.查看姓李的学生信息
  select * from student where name like '张%'
5.查看分数总和
 s elect  sum(grade) from student
6.查看平均分数
  select avg(grade) from student
7.查看最高分
  select max(grade) from student
8.查看那最低分
  select min(grade) from student
9.查看
10.分数升序排列
   select * from student order by grade asc
11.分数降序排列
  select * from student order by grade desc
12.筛选出分数排名为3-10条内容
   select * from student order by grade desc limit 2,8
   select a.* from (select * from student order by grade desc limit 4)  a order by grade asc  limit 2
 
13. 插入一条数据,学号9,名字王菲菲,分数73,性别女
    insert into students values (9,'王菲菲',73,'女')
 
14.修改 王菲菲的 分数为 94
update student set grade=94 where name='王菲菲'
二 多表
student
SidSnameSageSgender
1张三1991/10/9
2李四1992/3/10
3王五1990/5/1
4赵六1991/4/12
5孙丽1991/8/19
6王田1991/1/14
7李可1991/7/15
8杨庄1991/10/16
 
 
teacher
TidTname
1崔荷花
2张梅花
3王杏花
 
 
grade
SidCidscore
1180
2170
3190
41100
5188
6199
7166
8155
1256
2277
3295
4244
5290
6299
1343
2366
3388
4387
5385
6398
7396
 
 
Course
CidCnameTid
1语文1
2数学2
3英语3
 
1. 查询学生赵六id以及语数外成绩
  select a.Sid, b.score as '语文', c.score as '数学', d.score as '英语' from (select * from student where Sname='赵雷') a, sc b, sc c, sc d where a.Sid=b.Sid and b.Sid=c.Sid and c.Sid=d.Sid and b.Cid='01' and c.Cid='02' and d.Cid='03'
 
2. 查询所有同学的学生编号,学生姓名,选课总数,所有课程总成绩
  select a.Sid, a.Sname, co  unt(c.Cid) as '选课总数', sum(b.score) as '分数总和' from student a, grade b, Course c where a.Sid=b.Sid group by a.sid, b.Cid=c.Cid order by a.sid
 
3. 语文成绩比数学成绩高的学生信息
  select a.*,  b.score as '语文', c.score as '数学' from student a, grade b, grade c  where a.Sid=b.Sid and b.Sid = C.Sid and b.Cid='01' and c.Cid='02' and b.score > c.score
 
4.查询平均成绩大于等于85的所有学生的学号,姓名,平均成绩
  select a.Sid, a.Sname, avg(b.score) from student a, grade b where a.Sid = b.Sid group by a.Sid, a.Sname having avg(b.score) >= 85 order by a.Sid
 
5.查询出语数外三科的平均成绩
  select avg(a.score) as '语文', avg(b.score) as '数学', avg(c.score) as '英语' from grade a, grade b, grade c where a.Sid=b.Sid and b.Sid=c.Sid  and a.Cid='01' and b.Cid='02' and c.Cid='03'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值