dml测试软件,SQL脚本数据操纵语言DML举例

7bfaad4536bf61345993fcd91108f697.pngSQL脚本数据操纵语言DML (Data manipulation Language)

事先在SQL库中建立三个表:

student 表 包括字段:

number(学号) name(姓名) sex(性别) age(年龄) jiguan(籍贯) class(班级)

grade 表 包括字段:

number(学号) kcm(课程名) cj(成绩)

class表 包括字段:

kch(课程号) kcm(课程名)

数据根据自己的情况填写,以下的举例将根据数据的不同而显示结果不同。

显示表中所有的学生

select *from student

------------------------

精确查询张三

select *from student

where name='张三'

------------------------

精确查询张三,只显示name字段

select name from student

where name='张三'

------------------------

所有数据只显示name 字段

select name from student

------------------------

所有数据只显示姓名和籍贯字段

select name,jg from student

------------------------

显示郭晶晶的姓名和籍贯字段,

select name,jg from student

where name='郭晶晶'

------------------------

显示姓于的人的姓名和籍贯

模糊查询

统配符%

select name,jg from student

where name like '于%'

当用like 如果后面没有%,就相当于等值查询。

%代表任意数量的字符,_只代表一个字符。

------------------------

显示叫于*的人的姓名和籍贯

select name,jg from student

where name like '于_'

------------------------

显示叫于**的人的姓名和籍贯

select name,jg from student

where name like '于__'

------------------------

设计表的时候,数据类型使用varchar 不使用char .因为后者在表字段里出现空格。

------------------------

姓于的家在上海的

select name,jg from student

where name like '于%'

and jg='上海'

------------------------

显示年龄在20-22之间的学生

select * from student

where age>=20 and age<=22

------------------------

简化一下上题显示年龄在20-22之间的学生

select * from student

where age between 20 and 22

其中between  and包含20和22的。

如果不想包含边界,就用age>20 and age<22

------------------------

显示在003班,籍贯是辽宁的

select * from student

where jg='北京' and class='003'

不可以用between  and,因为不在一个字段中,

------------------------

籍贯是北京的,或籍贯是'上海'的

select * from student

where jg='北京' and jg='上海'

用between  and只适合于数值(不加单引号),日期(加单引号),货币类型(不加单引号)

------------------------

选择出生日期在1990-1-1和1998-1-1之间的人

select * from student

where birth between '1990-1-1'and '1997-2-1'

------------------------

选择姓于的或者年龄是22岁的人

select * from student

where name like '于'

or age=22

------------------------

选择年龄是20,21,22岁的人

select * from student

where age=22 or age=21 or age=20

------------------------

使用IN实现年龄是20,21,22岁的人

select * from student

where age in(20,21,22)

------------------------

select * from student

where age in(20,21,22,30)

------------------------

选择工资在1000到5000的人

select * from table1

where salary between 1000 and 5000

------------------------

选择籍贯在北京,上海和广东的人

select * from student

where jg in('北京','上海','广东')

------------------------

选择籍贯是空的记录

select * from student

where jg is null

------------------------

count 来统计满足条件的记录数

统计年龄大于20的有多少

select count(*) from student

where age>20

------------------------

统计年龄大于20的有多少,并且在得出的结果中给出显示结果的字段名命名,直观

select count(*) as 学生总数 from student

where age>20

------------------------

统计籍贯在上海的学生总数

select count(*) as 籍贯 from student

where jg='上海'

------------------------

统计姓刘的男生有几个

select count(*) as 姓氏 from student

where name like '刘%' and sex='男'

------------------------

统计班级001班,年龄在20-25岁之间的男生

select count(*) as 姓氏 from student

where class='001'

and sex='男'

and age between 20 and 25

------------------------

name 为SQL保留,最好不用,起其它的名字

不要把语句写在一行,方便找到问题出在哪里。一个用折行,或先写好有把握的语句,再一条的加,编

译,看问题出现在哪里。

------------------------

sum(),avg(),count(*),min(),max()

-------------

求年龄最大的男生的年龄

select max(age) from student

where sex='男'

------------------------

找出出生年月最早的学生

select min(birth) from table1

------------------------

找出年龄最小的籍贯是上海的学生

select min(age) from student

where jg='上海'

------------------------

求001班所有人的平均年龄

select avg(age) from student

where class='001'

------------------------

求课程号为G001的最低分

select min(cj) from grade

where kch='G001'

------------------------

求学号为001的人的所有课程的总成绩

select sum(cj) from grade

where studyno='001'

------------------------

GROUP BY

统计每班各有多少个学生,并分组显示

select count(*) from student

group by class

------------------------

统计每班各有多少个学生,并分组显示,并显示班级名称

select class,count(*) from student

group by class

显示出了班级名,

------------------------

select age,class,count(*) from student

group by class

错的

age没分组

------------------------

select age,class,count(*) from student

group by age,class

有一个组被打散了

------------------------

group by只出现一个

order by出现多个

------------------------

统计每门课的平均成绩

select  kch ,avg(cj) from grade

group by kch

------------------------

统计每门课程的平均成绩并按照(课程号)从大到小显示

select  kch ,avg(cj) from grade

group by kch

order by kch desc

------------------------

统计每门课程的平均成绩并按照(课程成绩)从小到大显示

select  kch ,avg(cj) from grade

group by kch

order by avg(cj) desc

------------------------

order by聚合函数

显示学号以及不及格科目的总数

select  studyno ,count(*)  from grade

where cj<=60

group by studyno

------------------------

显示有两门以上不及格的学生的学号

select  studyno from grade

where cj<=60

group by studyno

having count(*)>=2

having为分组后的条件筛选,可以加聚合函数,必须和group by配套使用。

------------------------

显示每门课程取得最高分的课程号和对应的课程成绩。

select  kch,max(cj)from grade

group by kch

------------------------

找出某个班有4个以上学生年龄超过20岁的

select  class from student

where age>=20

group by class

having count(*)>=4

------------------------

*显示英语课成绩最高的学生姓名

select name from student

where studyno=

(select studyno from grade

where cj=

(select max(cj) from grade

where kch=

(select kch from class

where kcm='英语'))and

kch=

(select kch from class

where kcm='英语'))

------------------------

*显示张三的英语课得了多少分

select cj from grade

where studyno=

(select studyno from student

where name='张三')and

kch=

(select  kch from class

where kcm='英语')

------------------------

显示001班的平均成绩

select avg(cj) from grade

where studyno in(

select studyno from student

where class='001')

------------------------

联结查询

起别名,有重名的要加表名

select a.number ,name,score,b.classnumber,classname

from student a,class b,grade c

where a.number=c.number

and b.classnumber=c.classnumber

and name='张三'and classname='英语'

------------------------

显示张三的英语成绩还有学号和班级号

select a.studyno ,name,cj,class

from student a,class b,grade c

where a.studyno=c.studyno

and b.kch=c.kch

and name='张三'and kcm='英语'

------------------------

显示001班英语最高分的学生姓名及分数

select name,score

from student a,class b,grade c

where a.number=c.number

and b.classnumber=c.classnumber

and a.score=(

select max(score) from grade)

and class='001'

and classname='英语'

------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值