sql语句练习题

1.创建表:
id,
标题title, 100字内,
公司f_id, 100以内数字,
类型f_type进口1, 出口2,
金额money,2位小数,
时间date;
并插入几条数据。

create table t_order(id int unsigned primary key auto_increment not null,
					title varchar(100) default '',
					f_id tinyint unsigned,
					f_type enum("1", "2"),
					money decimal(10, 2),
					date date);
insert into t_order(title, f_id, f_type, money, date) values
						  ('a', 5, "1", 100.11, "2020-8-31"),
						  ('b', 5, "2", 200.22, "2020-8-30"),
						  ('c', 6, "1", 300.33, "2020-8-31"),
						  ('d', 6, "2", 400.44, "2020-7-20"),
						  ('e', 7, "1", 500.55, "2020-6-20");
  1. 查询最近7天每个公司的进出口总金额。
select f_id, group_concat(date), group_concat(money), sum(money) from t_order where DATE_SUB(CURDATE(), INTERVAL 6 DAY) <= date group by f_id;
  1. 按月查询每个公司进口总金额,出口总金额。
方法一. 
select f_id, DATE_FORMAT(date,'%Y-%m') month, group_concat(date), group_concat(money), sum(money), f_type from t_order where f_type='1' group by f_id, month
union
select f_id, DATE_FORMAT(date,'%Y-%m') month, group_concat(date), group_concat(money), sum(money), f_type from t_order where f_type='2' group by f_id, month order by f_id, month;
方法二
select f_id, DATE_FORMAT(date,'%Y-%m') month, group_concat(date), group_concat(money), sum(money), f_type from t_order group by f_id, month, f_type order by f_id, month;

指定月份查询每个公司进口总金额,出口总金额:

方法一
select f_id, group_concat(date), group_concat(money), sum(money), f_type from t_order where f_type='1' and DATE_FORMAT(date,'%Y-%m')='2020-08' group by f_id
union
select f_id, group_concat(date), group_concat(money), sum(money), f_type from t_order where f_type='2' and DATE_FORMAT(date,'%Y-%m')='2020-08' group by f_id order by f_id;
方法二
select f_id, group_concat(date), group_concat(money), sum(money), f_type from t_order where DATE_FORMAT(date,'%Y-%m')='2020-08' group by f_id, f_type order by f_id;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值