2021-01-25

##sql创建学生表,成绩表 欢迎补充

CREATE TABLE Student(
sno int PRIMARY KEY auto_increment, sname VARCHAR(55) NOT NULL, sage INT NOT NULL);truncate table student;INSERT into student VALUES(1001,“赵”,18);INSERT into student VALUES(1002,“钱”,18);INSERT into student VALUES(1003,“孙”,25);INSERT into student VALUES(1004,“李”,25);INSERT into student VALUES(1005,“周”,29);INSERT into student VALUES(1006,“吴”,22);CREATE TABLE Course( cno INT PRIMARY KEY auto_increment, cname VARCHAR(55) NOT NULL);INSERT INTO course VALUES(1,“语文”);INSERT INTO course VALUES(2,“数学”);INSERT INTO course VALUES(3,“英语”);CREATE TABLE Score(sno int ,cno int ,score DOUBLE not null,PRIMARY KEY(sno,cno));-- CONSTRAINT fk_sno FOREIGN KEY(sno) REFERENCES student(sno)INSERT INTO score VALUES (1001,1,60);INSERT INTO score VALUES (1001,2,61);INSERT INTO score VALUES (1001,3,59);INSERT INTO score VALUES (1002,1,80);INSERT INTO score VALUES (1003,1,61);INSERT INTO score VALUES (1003,2,61);INSERT INTO score VALUES (1005,1,50);INSERT INTO score VALUES (1005,2,55);INSERT INTO score VALUES (1005,3,54);INSERT INTO score VALUES (1006,1,80);INSERT INTO score VALUES (1006,2,90);INSERT INTO score VALUES (1006,3,100);

create insert update

1、avg() max() min() sum() count()
—查询平均值,最大值,最小值,总和,总条数
select avg(s.sage) ,max(s.sage),min(s.sage),sum(s.sage),count(s.sno)
from student s

2、distinct
—查询不重复值
select distinct sage from student;
select count(distinct sage) from student;

3、where order by
—查询指定范围id内的数据
select sno,sname
from student
where sno in(2,5,6)/between/like
order by sno asc/desc

4、left join right join inner jion
—一科都没考的人
select stu.sno,stu.sname
from student stu
left join score sc
on stu.sno = sc.sno
where sc.sno is null;

5、简写
—参与考试的学生的成绩
select stu.sno,stu.sname,c.cname,s.score
from student stu,score s,course c
where stu.sno = s.sno and s.cno = c.cno
order by stu.sname,s.score desc;

select stu.sno,stu.sname,c.cname,s.score
from student stu 
left join score s on stu.sno = s.sno
left join course c on s.cno = c.cno
order by stu.sname,s.score desc

6、group by having
—查询平均分大于60分的学生编号,学生名,平均分
select s.sno,s.sname,avg(sc.score)
from score as sc,student as s
where s.sno = sc.sno
group by s.sno
having avg(sc.score) > 60;

select s.sno,s.sname,avg(sc.score) 
from score as sc 
inner join student as s 
on s.sno = sc.sno 
group by s.sno 
having avg(sc.score) > 60;	

7、子查询
—所有学生中有缺考的人
select stu.sno,stu.sname from student stu
left join score sc
on stu.sno = sc.sno
group by stu.sno
having count(sc.sno) <> (select count(*) from course);

8、—参加考试,但是有缺考的人
select stu.sno,stu.sname from student stu
left join score sc
on stu.sno = sc.sno
where sc.sno is not null
group by stu.sno
having count(sc.sno) <> (select count(*) from course);

9、—所有科目都不及格的人
select s.sname from student s where s.sno not in
(select s.sno from student s left join score sc on s.sno = sc.sno and sc.score > 60);

10、—查询所有学生的学号、姓名、选课数、总成绩;
select stu.sno,stu.sname,count(s.sno) ‘参考科目数’,sum(s.score) ‘总成绩’
from student stu
left join score s on stu.sno = s.sno
left join course c on s.cno = c.cno
group by stu.sno
order by sum(s.score) desc;

使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值