| 本次SQL语句练习使用的是国产的达梦数据库, 数据库数据和练习题目由@Teacher熊 提供
-- 创建用户表
create table vspace.i_user (
phone char(11) not null primary key,
password varchar(20) not null
);
-- 创建基本信息表
create table vspace.i_basic(
phone char(11) not null primary key,
name varchar(30) not null,
id_card varchar(18) not null,
birthday datetime not null,
reg_date datetime not null,
last_login_date datetime not null,
head_images varchar(100) not null
);
-- 创建商品分类表
create table vspace.i_category(
CATEGORY_ID int not null primary key,
CATEGORY_LEVEL int not null,
CATEGORY_NAME varchar(20) not null,
PARENT_ID int not null
);
-- 创建商品表
create table vspace.i_goods(
GOODS_ID bigint not null primary key,
GOODS_TITLE varchar(1024),
PRICE double,
DISCOUNT double,
SPECIFICATION varchar(100),
DESCRIPTION text,
CATEGORY_ID int,
AMMOUNT int,
IMAGE_URL text,
UP_DATE datetime,
DOWN_DATE datetime
);
-- 创建购物车信息表
create table vspace.i_cart(
CART_ID bigint not null primary key,
PHONE char(11),
JOIN_DATE datetime,
GOODS_ID bigint,
AMMOUNT int
);
-- 创建订单表
create table vspace.i_order(
ORDER_ID bigint not null primary key,
PHONE char(11),
ORDER_DATE datetime,
GOODS_ID bigint,
AMMOUNT int,
SUM_PRICE number(10,2),
ORDER_STATUS varchar(50),
RECEIVE_ADDR_ID bigint
);
-- 创建配送地址表
create table vspace.i_receive_addr(
ADDR_ID bigint not null primary key,
PROV varchar(50),
CITY varchar(50),
SECT varchar(50),
DETAIL varchar(100),
RECEIVE_NAME varchar(30),
RECEIVE_PHONE char(11),
OWN_USER_PHONE char(11)
);
alter table vspace.i_basic
add constraint FK_basic_phone
foreign key (phone) references vspace.i_user(phone);
alter table vspace.i_goods
add constraint FK_category_id
foreign key (category_id) references vspace.i_category(category_id);
alter table vspace.i_cart
add constraint FK_user_phone
foreign key (phone) references vspace.i_user(phone);
alter table vspace.i_cart
add constraint FK_goods_id
foreign key (goods_id) references vspace.i_goods(goods_id);
alter table vspace.i_receive_addr
add constraint FK_own_user_phone
foreign key(OWN_USER_PHONE) references vspace.i_user(phone);
select * from vspace.i_goods where price>=50 and price<=100
select * from vspace.i_goods where description like '%肉%';
select phone from (
select count(*) cnt, phone
from vspace.i_order
group by phone
) d1
where d1.cnt>11;
使用 top
, limit
和伪列三种方式实现商品信息的分页查询
select top 3 * from vspace.i_goods
where goods_id not in (
select top 12 goods_id from vspace.i_goods
);
select * from (
select rownum num , *
from vspace.i_goods
where rownum <= 15
)t where t.num > 12;
select * from (
select rownum num , *
from vspace.i_goods
where rownum <= 15
)t where t.num > 12;
select u.phone, u.password, b.reg_date, b.name
from vspace.i_user u, vspace.i_basic b
where u.phone = b.phone
select u.phone, u.password, b.reg_date, b.name
from vspace.i_user u inner join vspace.i_basic b on (u.phone = b.phone)
select u.phone, b.name, o.order_date, g.goods_title, o.ammount, o.sum_price
from vspace.i_user u, vspace.i_basic b, vspace.i_order o, vspace.i_goods g
where b.phone=o.phone and o.goods_id=g.goods_id
要求每页显示 7 条,显示第 5 页的数据
select o.order_id, b.id_card,
(select goods_title from vspace.i_goods where goods_id=o.goods_id),
o.ammount, o.sum_price
from vspace.i_order o, vspace.i_basic b
where o.phone=b.phone
limit 28, 7;
同步更新数据: 每个订单的总价 = 该订单中商品的单价 * 购买数量
update vspace.i_order o
set sum_price =
(select price from vspace.i_goods where goods_id=o.goods_id) * ammount;
commit;
select * from vspace.i_order;
-- 比如:第一批次
update vspace.i_order o
set sum_price =
(select price from vspace.i_goods where goods_id=o.goods_id) * ammount
where order_id in (
-- 使用分页查询技巧
select order_id from (
select rownum num, * from (
select * from vsapce.i_order
) t2 where rownum <= 100000
)t1 where tq.num >= 0
);
commit;
按照 parent_id
进行分组统计每一个一级类型下的子类型个数
select parent_id num from vspace.i_category
group by parent_id;
对上述结果进行筛选,得到最多的一级类型
select parent_id,count(category_id) num
from vspace.i_category
where parent_id != 0
group by parent_id;
可对第一步查询统计的结果进行降序排列取第一个
select parent_id, count(category_id) num
from vspace.i_category
where parent_id != 0
group by parent_id
order by count(category_id) desc
limit 0,1;
使用max()
聚合函数
select max(t.num)
from (
select parent_id,count(category_id) num
from vspace.i_category
where parent_id != 0
group by parent_id
) t;
```
使用子查询得到详细数据
select * from vspace.i_category
where category_id =(
select parent_id
from vspace.i_category
where parent_id != 0
group by parent_id
order by count(category_id) desc
limit 0,1
);
按照每年每种商品的销售数量(按照年份、编号、销量分组)
select year(order_date), goods_id, sum(ammount) total
from vspace.i_order
group by year(order_date), goods_id
按年份分组,使用 max()
聚合函数
select t.y, max(t.total) max_total
from (
select year(order_date) y, goods_id, sum(ammount) total
from vspace.i_order
group by year(order_date), goods_id
) t
group by t.y
将第一步结果作为虚拟表 a
, 第二步中结果当作虚拟表 b
, 然后对 a
和 b
表做内联查询
select a.* from (
select year(order_date) y, goods_id, sum(ammount) total
from vspace.i_order
group by year(order_date), goods_id
) a, (
select y, max(total) max_total
from (
select year(order_date) y, goods_id, sum(ammount) total
from vspace.i_order
group by year(order_date), goods_id
) t
group by y
) b
where a.y = b.y and a.total = b.max_total
同比数据分析
查询统计出去年每个季度销售总额
select datepart(QUARTER, order_date), sum(sum_price) total
from vsapce.i_order
where year(order_date) = year(now()) - 1
group by datepart(QUARTER, order_date);
查询统计出今年每个季度销售总额
select datepart(QUARTER, order_date), sum(sum_price) total
from vsapce.i_order
where year(order_date) = year(now())
group by datepart(QUARTER, order_date);
求增量
select a.season Season,
concat(round( (b.total - a.total) / a.total * 100, 2), '%') "Growth"
from (
select datepart(QUARTER, order_date) season, sum(sum_price) total
from vspace.i_order
where year(order_date) = year(now()) - 1
group by datepart(QUARTER, order_date)
) a, (
select datepart(QUARTER, order_date) season, sum(sum_price) total
from vspace.i_order
where year(order_date) = year(now())
group by datepart(QUARTER, order_date)
) b
where a.season = b.season
order by a.season asc;
统计每个用户今年消费总额
select phone, sum(sum_price) sp from vspace.i_order
where year(order_date) = year(now())
group by phone;
按照消费总数降序取出前十名
select phone, sum(sum_price) "Consumption"
from vspace.i_order
where year(order_date) = year(now()) - 1
group by phone
order by sum(sum_price) desc
limit 0, 10;
考虑消费总额相同的情况
使用 distinct
关键字去掉重复的总销售额, 然后降序去前10个销售额度
select distinct sum(sum_price) num from vspace.i_order
where year(order_date) = year(now())
group by phone
order by num desc limit 0, 10;
联查
select a.* from (
select sum(sum_price) num, phone
from vspace.i_order where year(order_date) = year(now())
group by phone
) a, (
select distinct sum(sum_price) num from vspace.i_order
where year(order_date) = year(now())
group by phone
order by num desc limit 0, 10
) b
where a.num = b.num
获取用户信息
select *
from vspace.i_basic b, (
select phone, sum(sum_price) "Consumption" from vspace.i_order
where year(order_date) = year(now()) - 1
group by phone
order by sum(sum_price) desc
limit 0, 10
)top10
where b.phone = top10.phone;
| 下架: 将商品下架日期设置为当前系统日期
查询每种商品的订单量
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
查询最少的订单量
select min(num)
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)
查询订单量最少的商品
select t1.goods_id, t2.min_num
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)t1, (
select min(num) min_num
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)
)t2
where t1.num = t2.min_num;
将销售订单量最少的商品下架
update vspace.i_goods
set down_date = curdate()
where goods_id in (
select t1.goods_id
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)t1, (
select min(num) min_num
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)
)t2
where t1.num = t2.min_num
);
查看是否修改成功
select *
from vspace.i_goods g, (
select t1.goods_id, t2.min_num
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)t1, (
select min(num) min_num
from (
select goods_id, sum(ammount) num
from vspace.i_order
group by goods_id
)
)t2
where t1.num = t2.min_num
)t
where t.goods_id = g.goods_id;
按年提取往年销售额
select year(order_date), sum(SUM_PRICE)
from vspace.i_order
where year(order_date) != year(now())
group by year(order_date);
提取今年销售额
select year(now()), sum(SUM_PRICE)
from vspace.i_order
where year(order_date)=year(now());
使用 往年数据与今年数据进行计算
select b.y YEAR,
concat(round((a.sp - b.sp) / b.sp * 100, 3), '%') "Growth Compared with 2020"
from (
select year(now()), sum(SUM_PRICE) sp
from vspace.i_order
where year(order_date)=year(now())
) a , (
select year(order_date) y, sum(SUM_PRICE) sp
from vspace.i_order
where year(order_date) != year(now())
group by year(order_date)
) b
order by b.y asc;
在大学数据库课程的学习中,主要关注的是数据库相关的理论知识以及SQL语句的基本用法。对于多个表联查练习得较少。通过本次练习,我对于数据库得数据查询操作更加熟练。对于复杂得查询,需要将查询操作一步一步拆分,最终通过 嵌套查询(子查询) 获得最终所需要的数据。