group by语句,join on ,left join on ,vue 的下拉框,按钮事件,方法的触发等。
最近项目里要写报表,我写了5-6张报表,包括sql语句和页面表格(vue实现).业务不是很复杂。最多就关联7-8张表。前面有点捉急,vue的很多不会,慢慢的网上查查,问下前端。功能基本能实现(三个功能)1.查询,2.选择条件(比如时间,部门,订单等)来查询3.导出信息。这里分享下最近一些知识点。
首先说下sql语句。看到一个需求,第一,要将需求分解。在多个字段中(如果是多张表)找到一个主表。然后以它为中心,去关联其他表。第二,是要找到关联条件。(如果实在找不到,自己可以添加字段关联。开发阶段如果允许的话)。第三,将涉及到的表的查询语句写出来(单表)。第四,将上述表的语句进行关联。第五,在数据库里进行测试,然后在代码里测试。
比如我这个 个人业绩查询报表:
//1.先查业务员,以客户为主表
select id,login_name,key_name,department_entity from user where status=1
//2.查订单表。按照 业务员分组
select handle_entity,sum(isnull(order_amount,0.0)) as order_amount,sum(isnull(medium_cost,0.0)+isnull(market_cost,0.0)) as cost from oms_order where order_category=1 and review_state in(3,4) and status=1 group by handle_entity
//3.查回款表 按照 业务员分组
select handle_entity,sum(isnull(money,0.0)) as receive_amount from ds_fin_reurn_money where review_state=2 and status=1 group by handle_entity
4.回款表 预回款
select handle_entity,sum(isnull(money,0.0)) as receive_amount_pre from ds_fin_rturn_money where review_state=1 and status=1 group by handle_entity
5.退款表
select sum(isnull(receive_amount,0.0)) as rec_no,handle_entity from d_fin_back_bill where status=1 group by handle_entity
//6.查部门
select key_name,id from sy_department where status=1
//综合
select a.login_name as work_number,a.key_name as sale_man,f.key_name as department,
b.order_amount,c.receive_amount,(c.receive_amount+d.receive_amount_pre-e.rec_no) as total_amount,
(c.receive_amount-b.cost) as return_maori from ( select id,login_name,key_name,department_entity from sys_user where status=1) a left join (select handle_entity,sum(isnull(order_amount,0.0)) as order_amount,sum(isnull(medium_cost,0.0)+isnull(market_cost,0.0)) as cost from ds_oms_order where review_state in(3,4) and status=1 group by handle_entity) b on a.id=b.handle_entity left join(select handle_entity,sum(isnull(money,0.0)) as receive_amount from ds_fin_return_money where review_state=2 and status=1 group by handle_entity) c on a.id=c.handle_entity left join (select handle_entity,sum(isnull(money,0.0)) as receive_amount_pre from ds_fin_return_money where review_state=1 and status=1 group by handle_entity) d on a.id=d.handle_entity left join (select sum(isnull(receive_amount,0.0)) as rec_no,handle_entity from ds_fin_back_bill where status=1 group by handle_entity) e on a.id=e.handle_entity left join (select key_name,id from sys_department where status=1) f on a.department_entity=f.id
语句有点啰嗦,但这样写的话,出了问题好定位一些。
一:这里谈谈正题group by。之前学过group by语句,通俗的将就是将数据进行分组。后面的接的参数就是按什么分组。比如user表,group by sex.就会有两组。男一组和女一组。还有记住一条,在聚合函数或者group by 语句中,在select后面的字段,如果不在聚合函数(sum(),avg(),max()等)中,就必须在group by 语句中。否则就会报错,这个先记住,后面写多了就会理解了。
现在如果group by 字段1,字段2。它首先按字段1进行分类。得到的结果再按字段2分类。
比如group by year,month.它会首先将结果按year分类,就会出现每一年的记录。然后再按month分类,就会出现每一月的记录。
其实,大可按照month分类。结果不会变,只是排列顺序不好看而已。
二:left join
这个用的比较多。先选择主表,然后一路left join 即可。及主表的每一条记录都会显示,从表如果关联不上就不会显示。
如果想只有满足条件的(主表)才显示,那么可以去掉left.
三:vue 作为后台开发,也在一点点自学vue了。还好有vue项目可以参考参考。
vue的话最好使用node.js,npm(cnpm)来操作。比如生成vue项目,导包 npm(cnpm) install,打包: (npm)cnpm run build,启动:nmp(cnpm) run dev 等。
其实我用vue时,都是参考下面这个element.里面有很多组件参考。基本都能满足需要。
vue的话一般分为三部分。template,script,style.就是展现层,逻辑层,样式层。
和以前的html中的三部分很像。目前我个人觉得它的v-model很好。很方便获取值。
主要说下几点:1.用的变量要在data--return 中定义
2.在下面的方法中使用变量时。需要用this.来获取。比如获取上图的department_entityField.this.department_entity.
3.比如你想做个清除查询条件的按钮。在按钮的方法中将条件的值赋值为''即可。
下面的图片是很好用的网址。同事们叫它 饿了么(element)。