mysql 中汇总配送商品销量 订单数 和订单总金额 下面是表结构

萧寒往往 2016-01-19 10:34:27

CREATE TABLE `order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_number` varchar(255) DEFAULT NULL COMMENT '订单编号',
`create_time` timestamp NULL DEFAULT NULL COMMENT '下单时间',
`status` char(1) DEFAULT NULL COMMENT '订单状态:1待付款2待发货3发货中4待评价5已完成6已取消7已删除',
`user_id` int(11) DEFAULT NULL,
`good_num` int(11) DEFAULT NULL COMMENT '商品总数量',
`pay_method` char(1) DEFAULT NULL COMMENT '支付方式1支付宝2微信3货到付款4余额',
`pay_number` varchar(255) DEFAULT NULL COMMENT '交易号',
`pay_time` timestamp NULL DEFAULT NULL COMMENT '支付时间',
`confirm_time` timestamp NULL DEFAULT NULL COMMENT '确认收货时间',
`cancel_time` timestamp NULL DEFAULT NULL COMMENT '取消订单时间',
`is_return` char(1) DEFAULT '0' COMMENT '1退款0不退款(默认)',
`avtive_content` varchar(255) DEFAULT NULL COMMENT '满就送的整条信息',
`descr` varchar(255) DEFAULT NULL COMMENT '备注',
`pay_money` float(20,2) DEFAULT NULL COMMENT '实际支付金额',
`jf` int(11) DEFAULT '0' COMMENT '使用积分0:未使用(默认)',
`money_used` float(20,2) DEFAULT NULL COMMENT '使用的金额',
`wuliu_fee` float(20,2) DEFAULT NULL COMMENT '运费',
`reciver_name` varchar(255) DEFAULT NULL COMMENT '收货人姓名',
`reciver_phone` varchar(255) DEFAULT NULL COMMENT '收货人电话',
`reciver_addr` varchar(255) DEFAULT NULL COMMENT '收货地址',
`send_time` varchar(255) DEFAULT NULL COMMENT '配送时间',
`sender_id` int(11) DEFAULT NULL COMMENT '配送人员',
`sum` float(20,2) DEFAULT NULL COMMENT '订单金额总计(总价+运费-积分-账户余额)',
`city_id` int(11) DEFAULT NULL COMMENT '城市ID',
`delivery_time` timestamp NULL DEFAULT NULL COMMENT '配送员配送时间',
`total` float(20,2) DEFAULT NULL COMMENT '订单总价(不包含运费)',
`weixin_code` varchar(255) DEFAULT NULL COMMENT '微信返回的CODE',
`hb` float(20,2) NOT NULL DEFAULT '0.00' COMMENT '红包金额',
`hb_id` int(11) DEFAULT NULL COMMENT '红包 id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1485 DEFAULT CHARSET=utf8;



CREATE TABLE `order_good` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL COMMENT '订单id',
`goods_id` int(11) DEFAULT NULL COMMENT 'order_goods表的id',
`good_number` int(11) DEFAULT NULL COMMENT '该商品的数量',
`create_time` timestamp NULL DEFAULT NULL COMMENT '订单提交时间',
`good_id` int(11) DEFAULT NULL,
`goods_price` float(11,0) DEFAULT NULL COMMENT 'order_goods表单价',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3646 DEFAULT CHARSET=utf8;



CREATE TABLE `order_goods` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`good_name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`descr` varchar(255) DEFAULT NULL COMMENT '简介',
`buy_price` float(20,2) DEFAULT '0.00' COMMENT '进价',
`now_price` float(20,2) DEFAULT NULL COMMENT '现价',
`old_price` float(20,2) DEFAULT NULL COMMENT '原价',
`price_change` char(1) DEFAULT '0' COMMENT '1升2将0无(默认)',
`image1` varchar(255) DEFAULT NULL,
`image2` varchar(255) DEFAULT NULL,
`image3` varchar(255) DEFAULT NULL,
`image4` varchar(255) DEFAULT NULL,
`image5` varchar(255) DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL COMMENT '商品图标',
`good_type` char(1) NOT NULL COMMENT '1普通商品2整箱3兑换商品',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
`menu_id` int(11) DEFAULT NULL COMMENT '菜谱id',
`city_id` int(11) DEFAULT NULL COMMENT '城市id',
`category_id` int(11) DEFAULT NULL COMMENT '分类id',
`tag` char(1) DEFAULT '0' COMMENT '标签(0无默认,1新品2热卖3推荐4特价int',
`sale_num` int(11) DEFAULT NULL COMMENT '销量',
`dis_count` int(11) DEFAULT NULL COMMENT '评价',
`template` text COMMENT '图文详情',
`good_code` varchar(255) DEFAULT NULL COMMENT '商品编码',
`is_sale` char(1) DEFAULT NULL COMMENT '0上架1下架',
`jf_price` int(11) DEFAULT NULL COMMENT '兑换所需积分int(good_type不为3时为null)',
`good_id` int(11) DEFAULT NULL COMMENT '原始goodID',
`good_number` int(11) DEFAULT NULL COMMENT '该商品的数量',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3689 DEFAULT CHARSET=utf8;

CREATE TABLE `good` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`good_name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`descr` varchar(255) DEFAULT NULL COMMENT '简介',
`buy_price` float(20,2) DEFAULT '0.00' COMMENT '进价',
`now_price` float(20,2) DEFAULT NULL COMMENT '现价',
`old_price` float(20,2) DEFAULT NULL COMMENT '原价',
`price_change` char(1) DEFAULT '0' COMMENT '1升2将0无(默认)',
`image1` varchar(255) DEFAULT NULL,
`image2` varchar(255) DEFAULT NULL,
`image3` varchar(255) DEFAULT NULL,
`image4` varchar(255) DEFAULT NULL,
`image5` varchar(255) DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL COMMENT '商品图标',
`good_type` char(1) NOT NULL COMMENT '1普通商品2整箱3兑换商品',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`menu_id` int(11) DEFAULT NULL COMMENT '菜谱id',
`city_id` int(11) DEFAULT NULL COMMENT '城市id',
`category_id` int(11) DEFAULT NULL COMMENT '分类id',
`tag` char(1) DEFAULT '0' COMMENT '标签(0无默认,1新品2热卖3推荐4特价int',
`sale_num` int(11) DEFAULT NULL COMMENT '销量',
`dis_count` int(11) DEFAULT NULL COMMENT '评价',
`template` text COMMENT '图文详情',
`good_code` varchar(255) DEFAULT NULL COMMENT '商品编码',
`is_sale` char(1) DEFAULT '0' COMMENT '0上架1下架',
`jf_price` int(11) DEFAULT NULL COMMENT '兑换所需积分int(good_type不为3时为null)',
`standard` varchar(255) DEFAULT NULL COMMENT '商品规格',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=617 DEFAULT CHARSET=utf8;



order,order_good,order_goods三个表是订单表
good是商品表

...全文
828 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2016-01-19
  • 打赏
  • 举报
回复
order_goods--看结构是进货表不是订单表吧
中国风 2016-01-19
  • 打赏
  • 举报
回复
order,order_good,order_goods三个表是订单表
order_goods--通过什么与order,order_good关联?除了有good_id与good关联,还有那个栏位
中国风 2016-01-19
  • 打赏
  • 举报
回复
引用 16 楼 u014051479 的回复:
...现在有提示 [Err] 1054 - Unknown column 'a.order_id' in 'on clause' 这个呢
栏位名输错a.id改改 select c.good_name,left(a.send_time,10) as send_time2 ,sum(b.good_number) as good_number ,count(distinct b.order_id) as OrderCount -- 订单数 ,sum(b.good_number*b.goods_price) as TotalAmount -- 订单总金额 from `order` as a inner join order_good as b on b.order_id=a.id inner join good as c on c.id=b.good_id group by c.good_name,left(a.send_time,10)
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
...现在有提示 [Err] 1054 - Unknown column 'a.order_id' in 'on clause' 这个呢
中国风 2016-01-19
  • 打赏
  • 举报
回复
引用 14 楼 u014051479 的回复:
还是 提示 1054 - Unknown column 'b.good_numbe' in 'field list'

少了r

select c.good_name,left(a.send_time,10) as send_time
,sum(b.good_number) as good_number
,count(distinct b.order_id) as OrderCount -- 订单数
,sum(b.good_number*b.goods_price) as TotalAmount -- 订单总金额
from `order` as a
inner join order_good as b on b.order_id=a.order_id
inner join good as c on c.id=b.good_id
group by c.good_name,left(a.send_time,10)
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
还是 提示 1054 - Unknown column 'b.good_numbe' in 'field list'
中国风 2016-01-19
  • 打赏
  • 举报
回复
引用 11 楼 u014051479 的回复:
send_time 是这种格式的 2015-12-16(下午) 我用convert(varchar(20),send_time,112) 这个判断不了啊
你是mysql--这样没报错? --用left
select  c.good_name,left(send_time,10) as send_time
,sum(b.good_number) as good_number
,count(distinct b.order_id) as OrderCount -- 订单数
,sum(b.good_numbe*b.goods_price) as TotalAmount -- 订单总金额
from `order` as a 
inner join order_good as b on b.order_id=a.order_id
inner join good as c on c.id=b.good_id
group by  c.good_name,left(send_time,10)
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
[Err] 1054 - Unknown column 'b.good_numbe' in 'field list' 这个是mysql
中国风 2016-01-19
  • 打赏
  • 举报
回复
引用 7 楼 u014051479 的回复:
还有 send_time 是这种格式的 2015-12-16(下午)
这样加栏位显示
select  c.good_name,cast(send_time as date) as send_time
,sum(b.good_number) as good_number
,count(distinct b.order_id) as OrderCount -- 订单数
,sum(b.good_numbe*b.goods_price) as TotalAmount -- 订单总金额
from `order` as a 
inner join order_good as b on b.order_id=a.order_id
inner join good as c on c.id=b.good_id
group by  c.good_name,cast(send_time as date)
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
怎么判断日期啊 要统计一个日期范围内的、
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
send_time 是这种格式的 2015-12-16(下午) 我用convert(varchar(20),send_time,112) 这个判断不了啊
中国风 2016-01-19
  • 打赏
  • 举报
回复
看结果集应该只需要order_good 、good 表,如果需要在order表加条件时如下,不加时可去掉
select  c.good_name
,sum(b.good_number) as good_number
,count(distinct b.order_id) as OrderCount -- 订单数
,sum(b.good_numbe*b.goods_price) as TotalAmount -- 订单总金额
from `order` as a 
inner join order_good as b on b.order_id=a.order_id
inner join good as c on c.id=b.good_id
group by  c.good_name
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
还有 send_time 是这种格式的 2015-12-16(下午)
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
要查询一下 销量 就是

正宗原生态赣南脐橙不染色不打蜡	2	31.60	[查看详情]
沈师傅夹心蛋香菇肉丸36g	2	4.60	[查看详情]
四川甜枇杷6个/盒	1	15.60	[查看详情]
莱阳丰水梨2个装	3	12.60	[查看详情]
库尔勒香梨2斤/份	1	10.80	[查看详情]
精品烟台红富士85#3斤装	2	21.60	[查看详情]
沾化冬枣大个1斤装	1	5.60	[查看详情]
海南青金桔1斤装无核甘甜	1	13.80	[查看详情]
白心大个火龙果1斤多/个	2	13.60	[查看详情]
精选海南香蕉3斤装	3	30.00	[查看详情]
精品徐香猕猴桃中号2斤装	2	25.60	[查看详情]
台湾释迦果1斤左右(后熟水果放软才好吃)	1	19.80	[查看详情]
枣夹核桃250g/份	2	39.60	[查看详情]
马来西亚进口凤梨2斤以上	1	12.80	[查看详情]
俄罗斯进口糖果​紫皮155g	1	9.90	[查看详情]
蒙牛大果粒风味发酵乳黄桃味260g	2	13.60	[查看详情]
这种类型的一个 表 还要查询下 总的订单数 ,订单总金额 这两个分开来。
中国风 2016-01-19
  • 打赏
  • 举报
回复
你需要显示的结果集栏位是? 就3栏位商品销量 订单数 和订单总金额 不按商品
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
order_good 里面的order_id 是关联 order里面的id
萧寒往往 2016-01-19
  • 打赏
  • 举报
回复
应该是 good_id 是关联商品的表

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧