订单表tbl_order
用户表tbl_user
1、查询2017年上半年(1-6月),上海地区销售额排名前10的商品ID。需要的字段:商品ID
2、查询2017年7月所有订单中,有且仅有轮胎和保养两个品类的订单数1. selcet pid from tbl_order o left join tbl_user u on o.user_id=u.user_id where u.city='上海' and month(o.order_date)<=6 order by sum(salesamout) group by pid limit 10 2.select count(*) from tbl_userwhere category in ('轮胎','保养') and month(order date)=7 and year(order date)=2017
1) 方法1: select pid from ( select pid, sum(salesamount) as sales, dense_rank() over(order by sum(salesamount) desc) as ranking from tbl_order as t1 left join tbl_user as t2 on t1.userid= t2.userid where City = "上海市" and month(orderdate) between 1 and 6 and year(orderdate) = 2017 group by pid ) as t where ranking <= 10 方法2: select pid from tbl_order as t1 left join tbl_user as t2 on t1.userid= t2.userid where City = "上海市" and month(orderdate) between 1 and 6 and year(orderdate) = 2017 group by pid order by sum(salesamount) desc limit 10 2)select count(distinct orderid)from (select orderid, count(distinct category) as cat_numfrom tbl_orderwhere year(orderdate) = 2017and category in ("轮胎", "保养")group by orderid) as twhere cat_num = 2
(1)select t1.pid from tbl_order t1
left join tbl_user t2 on t1.userid=t2.userid
where t2.City='上海市' and year(t1.orderdate)=2017 and month(t1.roderdate) between 1 and 6 #month和year函数可以返回日期的月和年
group by t1.pid
order by sum(t1.salesamout) desc # order by 后面可以跟聚合函数,但是group by 和where后面不能直接跟聚合函数,group by 的having后面可以加
limit 10
(2)select count(orderid) as cnt from tbl_order
where year(orderdate)=2017 and month(roderdate)=7
and category in ('轮胎','保养')