统计订单销售历史表中每个店铺中每种商品销售数量最多的用户的信息

类似查询订单销售历史表中每个店铺中每个商品销售数量最多的用户的信息(多表查询:查询的数据包含用户表,销售订单历史表,商品表,店铺表等表中的信息)
//method1 先根据条件查询出记录,然后用查出来的记录,去联合查询需要查询信息的表
select * from
( select t.sale_qty,t.item_id,t.store_id ,t.last_modify_user from sd_sales_order_his t
where t.sale_qty =(
select max(sd_sales_order_his.sale_qty) from sd_sales_order_his
where sd_sales_order_his.item_id = t.item_id and sd_sales_order_his.store_id=t.store_id
)) t
left join tbl_user e on e.user_id = t.last_modify_user
left join sd_store c on t."store_id" = c."store_id"
left join sd_item d on t."item_id" = d."item_id"

//method2 先把需要查询的几个表关联成一个临时的表,里面存放需要查询的所有字段,然后再根据条件过滤查询记录
select * from (select sd_sales_order_his.sale_qty,d.item_id,d.item_code,e.user_name,c.store_id from sd_sales_order_his
left join sd_store c on sd_sales_order_his."store_id" = c."store_id"
left join sd_item d on sd_sales_order_his."item_id" = d."item_id"
left join tbl_user e on e.user_id = sd_sales_order_his.last_modify_user) temp
where temp.sale_qty=
(select max(sd_sales_order_his.sale_qty) from sd_sales_order_his
where item_id = temp.item_id and store_id=temp.store_id)

//method3 先把需要查询的几个表关联成一个临时的表,里面存放需要查询的所有字段,然后再根据条件过滤查询记录
select * from (select sd_sales_order_his.sale_qty,d.item_id,d.item_code,e.user_name,c.store_id from sd_sales_order_his
left join sd_store c on sd_sales_order_his."store_id" = c."store_id"
left join sd_item d on sd_sales_order_his."item_id" = d."item_id"
left join tbl_user e on e.user_id = sd_sales_order_his.last_modify_user) temp
where not exists
(select 1 from sd_sales_order_his
where item_id = temp.item_id and store_id=temp.store_id and sale_qty>temp.sale_qty)

//类似查询订单销售历史表中每个店铺中每个商品销售数量最多的用户的信息(单表查询:查询的数据只包含用户表,不包括销售订单历史表,商品表,店铺表等表中的信息)
select * from tbl_user where tbl_user.user_id in
( select last_modify_user from sd_sales_order_his t where sale_qty =(
select max(sd_sales_order_his.sale_qty) from sd_sales_order_his
where sd_sales_order_his.item_id = t.item_id and sd_sales_order_his.store_id=t.store_id
))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值