3.2 从关系型数据库中查询数据

筛选数据--插入选课数据

select version();

select now();

select sysdate from dual;

select stuname ,stubirth from tb_student stusex=0;

----查询80后学生的信息----两种方法

select stuname ,stusex,stubirth  from tb_student where 

stubirth >='1980-1-1' and stubirth <='1989-12-31'

select stuname ,stusex,stubirth  from tb_student where 

stubirth between '1980-1-1' and '1989-12-31'

---查询姓“林”的学生和性别--模糊查询----

select stuname ,stusex from tb_student where stuname like '林%';

select stuname ,stusex from tb_student where stuname like '林_ _';

---查询有“不”的学生和性别--模糊查询----

select stuname ,stusex from tb_student where stuname like '%不%' or stuname like '%嫣%';

%匹配任意个字符,_ 精确匹配字符个数

---查询没有录入家庭地址的学生---处理空值用is,不能用=

select stuname from tb_student where stuaddr  is null;

select stuname from tb_student where stuaddr  is not  null;

---查询学生选课的所有日期(去重)

select distinct scdate from tb_score;

---查询学生的家庭住址(去重)

select distinct stuaddr from tb_student where stuaddr is not null;

---查询男学生的姓名和生日按照年龄从大到小排列(排序)

select stuname ,stubirth from tb_student where stusex=1 order by stubirth desc

select stuname ,year(now())-year(stubirth) as 年龄 from tb_student where stusex=1 order by 年龄 desc

---查询年龄最大的学生的出生日期(聚合函数)

---查询年龄最小的学生的出生日期(聚合函数)

select max (stubirth) from tb_student

select min (stubirth) from tb_student

---查询男女学生的人数---(分组和聚合函数)

select count(*) from tb_student;

select count(stuid) from tb_student;

select stusex,count(stusex) from tb_student group by stusex;

select stusex ,min(stubirth) from tb_student group by stusex;

----查询课程编号为1111的课程的平均成绩

select avg(scmark) from tb_score where couid=1111;

select min(scmark) from tb_score where couid=1111;

select count(scmark) from tb_score where couid=1111;

----先筛选--在分组--在排序---

----查询学号为1001的学生所有的课程的平均分

select avg(scmark) from tb_score where stuid =1001;

select stuid as 学号,avg(scmark) as 平均分 from tb_score group by stuid

---查询年龄大于等于90分的学生的学号和平均成绩

select stuid as 学号,avg(scmark) as 平均分 from tb_score group by stuid having 平均分>=90

---查询年龄最大的学生的姓名和年龄(子查询+运算)/嵌套查询

select  stuname as 姓名,year(now())-year(stubirth) as 年龄 from tb_student where stubirth =(select min(stubirth) from tb_student) ;

---查询选了两门以上课程的学生的姓名(子查询/分组条件/集合运算)

select stuname from tb_student where stuid in (

select stuid from tb_score group by stuid having count (stuid)>2

) ;

---查询学生姓名

import itertools

for val in itertools.permutations("ABCD",2):

         print(val)

for val in itertools.permutations("ABCD",'1234'):

         print(val)-------笛卡尔积 

内查询---外查询---

分页查询----

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值