mysql常见

CREATE TABLE t_Shop(
 id int  NOT NULL,
 商品名称 varchar (50) NOT NULL,
 商品编号 varchar(50) NOT NULL,
 销售数量 int NOT NULL,
 商品单价 int NOT NULL,
 销售地点 varchar(50) NOT NULL,
 销售日期 varchar(50) NOT NULL,
 销售人 varchar(50) NOT NULL
)
insert into t_Shop values(1,'可口可乐','100101',7,36,'新职宿舍四号楼','2019-4-5','张三');
insert into t_Shop values(2,'老坛酸菜牛肉面','110101',2,24,'新职宿舍一号楼','2019-4-5','张三');
insert into t_Shop values(3,'哇哈哈AD钙奶','100201',3,18,'新职宿舍五号楼','2019-4-5','张三');
insert into t_Shop values(4,'乐视薯片','140101',5,12,'新职宿舍四号楼','2019-4-5','张三');
insert into t_Shop values(5,'青岛啤酒','100301',3,30,'新职宿舍一号楼','2019-4-5','张三');
insert into t_Shop values(6,'傻子瓜子','120101',4,25,'新职宿舍五号楼','2019-4-5','张三');
insert into t_Shop values(7,'猫屎咖啡','130101',7,80,'新职宿舍一号楼','2019-4-5','张三');
insert into t_Shop values(8,'可口可乐','100101',7,36,'新职宿舍四号楼','2019-4-5','李四');
insert into t_Shop values(9,'老坛酸菜牛肉面','110101',2,24,'新职宿舍一号楼','2019-4-5','李四');
insert into t_Shop values(10,'哇哈哈AD钙奶','100201',3,18,'新职宿舍五号楼','2019-4-5','李四');
insert into t_Shop values(11,'乐视薯片','140101',7,12,'新职宿舍四号楼','2019-4-5','李四');
insert into t_Shop values(12,'青岛啤酒','100301',2,30,'新职宿舍一号楼','2019-4-5','李四');
insert into t_Shop values(13,'傻子瓜子','120101',3,25,'新职宿舍五号楼','2019-4-5','李四');
insert into t_Shop values(14'猫屎咖啡','130101',5,80,'新职宿舍一号楼','2019-4-5','李四');
insert into t_Shop values(15,'可口可乐','100101',8,36,'新职宿舍四号楼','2019-4-5','王五');
insert into t_Shop values(16,'老坛酸菜牛肉面','110101',12,24,'新职宿舍一号楼','2019-4-5','王五');
insert into t_Shop values(17,'哇哈哈AD钙奶','100201',5,18,'新职宿舍五号楼','2019-4-5','王五');
insert into t_Shop values(18,'乐视薯片','140101',6,12,'新职宿舍四号楼','2019-4-5','王五');
insert into t_Shop values(19,'青岛啤酒','100301',3,30,'新职宿舍一号楼','2019-4-5','王五');
insert into t_Shop values(20,'傻子瓜子','120101',3,25,'新职宿舍五号楼','2019-4-5','王五');
insert into t_Shop values(21,'猫屎咖啡','130101',4,80,'新职宿舍一号楼','2019-4-5','王五');
insert into t_Shop values(22,'可口可乐','100101',5,36,'新职宿舍四号楼','2019-4-5','赵四');
insert into t_Shop values(23,'老坛酸菜牛肉面','110101',2,24,'新职宿舍一号楼','2019-4-5','赵四');
insert into t_Shop values(24,'哇哈哈AD钙奶','100201',8,18,'新职宿舍五号楼','2019-4-5','赵四');
insert into t_Shop values(25,'乐视薯片','140101',4,12,'新职宿舍四号楼','2019-4-5','赵四');
insert into t_Shop values(26,'青岛啤酒','100301',3,30,'新职宿舍一号楼','2019-4-5','赵四');
insert into t_Shop values(27,'傻子瓜子','120101',3,25,'新职宿舍五号楼','2019-4-5','赵四');
insert into t_Shop values(28,'猫屎咖啡','130101',5,80,'新职宿舍一号楼','2019-4-5','赵四');

insert into t_Shop values(29,'可口可乐','100101',6,36,'新职宿舍一号楼','2019-4-6','王五');
insert into t_Shop values(30,'可口可乐','100101',6,36,'新职宿舍二号楼','2019-4-6','王五');
insert into t_Shop values(31,'可口可乐','100101',6,36,'新职宿舍三号楼','2019-4-6','王五');
insert into t_Shop values(32,'可口可乐','100101',6,36,'新职宿舍四号楼','2019-4-6','王五');
insert into t_Shop values(33,'可口可乐','100101',7,36,'新职宿舍一号楼','2019-4-6','李四');
insert into t_Shop values(34,'可口可乐','100101',6,36,'新职宿舍二号楼','2019-4-6','李四');
insert into t_Shop values(35,'可口可乐','100101',7,36,'新职宿舍三号楼','2019-4-6','李四');
insert into t_Shop values(36,'可口可乐','100101',6,36,'新职宿舍四号楼','2019-4-6','李四');
insert into t_Shop values(37,'可口可乐','100101',16,36,'新职宿舍一号楼','2019-4-6','张三');
insert into t_Shop values(38,'可口可乐','100101',6,36,'新职宿舍二号楼','2019-4-6','张三');
insert into t_Shop values(39,'可口可乐','100101',8,36,'新职宿舍三号楼','2019-4-6','张三');
insert into t_Shop values(40,'可口可乐','100101',6,36,'新职宿舍四号楼','2019-4-6','张三');
# 1、写出热销商品排名表
select
'商品名称',
sum(销售数量) 销售总数量
from t_Shop sp
group by '商品名称'
order by 销售总数量 desc;
# 2、统计销售总价超过3000元的商品名称和销售总价,并按照销售总价降序排序
select
'商品名称',
sum(销售数量*商品单价) 销售总价
from t_Shop sp
group by '商品名称'
order by 销售总价 desc;
# 3、统计各个宿舍楼对可口可乐的喜爱程度,即统计每个宿舍购买可口可乐的数量
select
销售地点,
sum(sp.销售数量) 数量
from t_Shop sp
where sp.商品名称 = '可口可乐'
group by sp.销售地点;
-- 二、有以下表结构:
-- 创建学生表
create table t_Student(
 S   varchar(20), -- 学生编号
 SN   varchar(20), -- 学生姓名
 SA   int,   -- 学生年龄
 SD   varchar(100) -- 所属单位
)
-- 创建选修课程表
create table t_Course(
 C  int,   -- 选修课程编号
 CN  varchar(50), -- 选修课程名字
 CT  varchar(20)  -- 任课老师
 
)
-- 创建成绩表
create table t_Score(
 S varchar(20),  -- 学生编号
 C int,    -- 选修课程编号
 G  int    -- 成绩
)
-- 1、使用子查询出选修课程名称为数学的学员学号和姓名
select * from student stu
where stu.S in(
  select sc.s from score sc
  where sc.c = (
   select c.c from course c
   where c.cn= '数学'
)
)
-- 2、查询选修课程超过5门的学员学号和所属单位
select S,SD from t_Student
where S in(
select S from t_Score
group by S
having count(distinct C)>5
);
-- 3、查询两门以上(含两门)课程不及格的学生姓名及其平均成绩
select s.SN, avg(sc.G) from t_student s,t_score sc
where
     s.S = sc.S and sc.G < 60
group by sc.s
having count(*)>1;
-- 4、查询出英语成绩比语文成绩高的学生学号,以及英语和语文成绩
select stu.S,yy.G as 英语,yw.G as 语文 from
    t_Student stu,
        (select * from t_Score
            where C = (select C from t_Course where CN='语文'))as yw,
        (select * from t_Score
            where C = (select C from t_Course where CN='英语'))as yy
        where stu.S = yw.S
        and yw.S=yy.S
        and yy.G>yw.G
-- 5、查询同时选修了历史和地理的所有学生姓名
-- 复杂知识 intersect
select sc1.S from t_score sc1
inner join t_course c
on sc1.C = c.C
where c.CN = '历史'
intersect
select sc.S from t_score sc
inner join t_course c
on sc.C = c.C
where c.CN = '地理';
posted @ 2020-09-08 17:46  .Liu  阅读(98)  评论(0编辑  收藏  举报