子查询
1.请用子查询查找年龄最小的用户的基本信息
--1.请用子查询查找年龄最小的用户的基本信息
SELECT * FROM I_BASIC
WHERE BIRTHDAY IN
(
SELECT MAX(I_BASIC.BIRTHDAY) FROM I_BASIC
--WHERE a.BIRTHDAY = I_BASIC.BIRTHDAY
);
- 请用子查询查找所有商品以及该商品所属的一级类型名称
select GOOD_TITLE, CATEGORY_ID
from I_GOODS
where CATEGORY_ID in(
select CATEGORY_ID
from I_GOODS);
- 分页查询订单编号、用户身份证号、商品名称、商品数据量、总价,要求每页显示7条,显示第5页的数据
--3.分页查询订单编号、用户身份证号、商品名称、商品数据量、总价,要求每页显示7条,显示第5页的数据
select * from
(
select ORDER_ID,GOODS_ID,AMMOUNT,SUM_PRICE
from I_ORDER U1 ,I_BASIC U2
WHERE U1.PHONE=U2.PHONE
)limit 28,7;
8.1_2 查询特例应用
- 特殊多表联查应用举例
- 球队比赛案例
- 球队信息表
- 赛事信息表
- 练习题:
- 查询球队赛程信息表:比赛时间 主队名称 客队名称 比赛结果
- 球队比赛案例
select s.GAME_DATE, h.TEAM_NAME,g.TEAM_NAME,
s.HOST_SCORE,s.GUEST_SCORE from g_schedule s,g_team h,
g_team g where s.HOST_TEAM=h.TEAM_ID and s.GUEST_TEAM=g.TEAM_ID;
8.3_4 Vspace项目复杂业务
- 查询子类型最多的一级商品类型
--1.查询子类型最多的一级商品类型
select * from I_CATEGORY
WHERE CATEGORY_ID in
(
SELECT PARENT_ID FROM I_CATEGORY
WHERE PARENT_ID != 0
GROUP BY PARENT_ID
HAVING COUNT(CATEGORY_ID)=
(
SELECT max(sln) maxsln from
(
select PARENT_ID,count(CATEGORY_ID) sln
from I_CATEGORY
WHERE PARENT_ID != 0 GROUP BY PARENT_ID
) t
)
);
- 查询显示每个月份销量最好的商品信息
select * from I_GOODS
WHERE GOODS_ID=
(
SELECT GOODS_ID FROM I_ORDER
GROUP BY GOODS_ID
ORDER BY SUM(GOODS_ID) DESC LIMIT 0,1
);
- 将销售订单量最少的商品下架(下架是指: 将商品下架日期设置为当前系统日期)
update i_goods set down_date=curdate() where goods_id in(
select goods_id from i_order group by goods_id
having count(order_id)=(
select min(orderNum) from (
select goods_id,count(order_id) orderNum
from i_order group by goods_id
) t
)
);
commit;
- 查询用户15112345678购买数量最多的商品的一级类型
select * from i_category where category_id=(
select parent_id from i_category where category_id=(
select category_id from i_goods where goods_id=(
select goods_id from i_order
where phone='15112345678' group by goods_id order
by sum(ammount) desc limit 0,1
)
)
);
- 根据第4步的查询结果给15112345678这个用户推送他喜欢购买的类型的商品中销量最好两种商品给他
--5.根据第4步的查询结果给15112345678这个用户推送他喜欢购买的类型的商品中销量最好两种商品给他
SELECT TOP 2 * FROM I_GOODS INNER JOIN I_ORDER ON I_GOODS.GOODS_ID = I_ORDER.GOODS_ID
WHERE CATEGORY_ID = 4
ORDER BY I_ORDER.AMMOUNT