###

  使用了临时表、又分组又连表的感觉好难写,使用拉 ravel

  但是现在越来也相信,没解决一个新的难题,自己又进步了一点点

###

原生的sql:

 select user_code, realname,sum(points) sum_points, user_id,source_type, t.is_verify, count(source_type) counts from
(select * from point_logs where source_type in ('layer', 'manager' , 'chief') ) t
left join users as u on u.id = t.user_id
where t.is_verify BETWEEN 1 and 2
GROUP BY user_id, source_type, t.is_verify

获得测试数据结果:

转换成laravel 的写法:

     $point_logs =  DB::table('point_logs')
->whereIn('source_type', ['layer', 'manager', 'chief'])
->whereBetween('is_verify', [1, 2]);
$report_infos = DB::table(DB::raw("({$point_logs->toSql()}) as p"))
->mergeBindings($point_logs)
->leftJoin('users', 'users.id', '=', 'p.user_id')
->select(
'users.realname',
'users.phone',
'users.user_code',
DB::raw('sum(points) as sum_points'),
'source_type',
DB::raw('count(source_type) as number'),
'p.is_verify'
)
->groupBy('user_id')
->groupBy('source_type')
->groupBy('p.is_verify');

改进

            $point_logs =  DB::table('point_logs')
->whereIn('source_type', [‘layer’, 'manager', 'chief'])
->whereBetween('is_verify', [1, 2]);
$report_infos = DB::table(DB::raw("({$point_logs->toSql()}) as p"))
->mergeBindings($point_logs)
->leftJoin('users', 'users.id', '=', 'p.user_id')
->select(
'users.realname',
'users.phone',
'users.user_code',
DB::raw('sum(points) as sum_points'),
'source_type',
DB::raw("case when source_type = 'layer' then '层级奖' when source_type = 'manager' then '经理奖' when source_type = 'chief' then '总监奖' end as 'source_type' "),
DB::raw('count(source_type) as number'),
'p.is_verify'
)
->groupBy(['user_id', 'source_type', 'p.is_verify'])
->orderBy('user_id');

前台页面展示--js 当元格合并处理

单元格行合并方法:

   table_rowspan("#assign_table", 1);
table_rowspan("#assign_table", 2);
table_rowspan("#assign_table", 3);
//函数说明:合并指定表格(表格id为table_id)指定列(列数为table_colnum)的相同文本的相邻单元格
//参数说明:table_id 为需要进行合并单元格的表格的id。如在HTMl中指定表格 id="table1" ,此参数应为 #table1
//参数说明:table_colnum 为需要合并单元格的所在列。为数字,从最左边第一列为1开始算起。
function table_rowspan(table_id, table_colnum) {
table_firsttd = "";
table_currenttd = "";
table_SpanNum = 0;
colnum_Obj = $(table_id + " tr td:nth-child(" + table_colnum + ")");
colnum_Obj.each(function (i) {
if (i == 0) {
table_firsttd = $(this);
table_SpanNum = 1;
} else {
table_currenttd = $(this);
if (table_firsttd.text() == table_currenttd.text()) {
table_SpanNum++;
table_currenttd.hide(); //remove();
table_firsttd.attr("rowSpan", table_SpanNum);
} else {
table_firsttd = $(this);
table_SpanNum = 1;
}
}
});
}

此外还有列合并的方法:

  //函数说明:合并指定表格(表格id为table_id)指定行(行数为table_rownum)的相同文本的相邻单元格
//参数说明:table_id 为需要进行合并单元格的表格id。如在HTMl中指定表格 id="table1" ,此参数应为 #table1
//参数说明:table_rownum 为需要合并单元格的所在行。其参数形式请参考jQuery中nth-child的参数。
// 如果为数字,则从最左边第一行为1开始算起。
// "even" 表示偶数行
// "odd" 表示奇数行
// "3n+1" 表示的行数为1、4、7、10.......
//参数说明:table_maxcolnum 为指定行中单元格对应的最大列数,列数大于这个数值的单元格将不进行比较合并。
// 此参数可以为空,为空则指定行的所有单元格要进行比较合并。
function table_colspan(table_id, table_rownum, table_maxcolnum) {
if (table_maxcolnum == void 0) {
table_maxcolnum = 0;
}
table_firsttd = "";
table_currenttd = "";
table_SpanNum = 0;
$(table_id + " tr:nth-child(" + table_rownum + ")").each(function (i) {
row_Obj = $(this).children();
row_Obj.each(function (i) {
if (i == 0) {
table_firsttd = $(this);
table_SpanNum = 1;
} else if ((table_maxcolnum > 0) && (i > table_maxcolnum)) {
return "";
} else {
table_currenttd = $(this);
if (table_firsttd.text() == table_currenttd.text()) {
table_SpanNum++;
table_currenttd.hide(); //remove();
table_firsttd.attr("colSpan", table_SpanNum);
} else {
table_firsttd = $(this);
table_SpanNum = 1;
}
}
});
});
}

laravel sql复杂语句,原生写法----连表分组的更多相关文章

  1. SQL判断语句用法和多表查询

    1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...

  2. Laravel SQL 查询语句集锦

    1.从数据表中取得单一数据列 $user= DB::table('users')->where('name','John')->first(); 2.检索表中的所有行 复制代码代码如下: ...

  3. SQL Server语句创建数据库和表——并设置主外键关系

    简单的创建数据库的 SQL 语句: use master go if exists(select * from sysdatabases where name='Test') begin select ...

  4. 关于同时查询父子名称的SQL查询语句的写法 id name parentId parentName .

    parentid是1就是id为1的公司的子公司 如图 查询出所有的信息后 由于我要呈现的是parentName 不是parentId所以想问下SQL语句怎么写 谢谢啦~~:) 解法: SELECT s ...

  5. SQL基本语句:2.基本表

    SQL基本表的增删改

  6. SQL删除语句同时向备份表插入数据

    从这里摘抄下来的,觉得很不错,http://www.cnblogs.com/ljhdo/p/5792886.html#3503524 ,以后就用这种方式删除,再也不用担心删除错数据啦!!!

  7. Oracle和sql server中复制表结构和表数据的sql语句

    在Oracle和sql server中,如何从一个已知的旧表,来复制新生成一个新的表,如果要复制旧表结构和表数据,对应的sql语句该如何写呢?刚好阿堂这两天用到了,就顺便把它收集汇总一下,供朋友们参考 ...

  8. ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法

    ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法 有些情况下,SQL SERVER 2008r2中需要保存float,int类型的数据,当C 中的变量为double ...

  9. SQL Server中查询数据库及表的信息语句

    /* -- 本文件主要是汇总了 Microsoft SQL Server 中有关数据库与表的相关信息查询语句. -- 下面的查询语句中一般给出两种查询方法, -- A方法访问系统表,适应于SQL 20 ...

随机推荐

  1. Android弹出窗口

    protected void PopUp() { final PopupWindow popup = new PopupWindow(TestActivity.this); View popView ...

  2. Baidu 人脸识别FireFly 与PC连接调试

    1.USB线插到离屏幕较远的双层USB口上方.2.安装驱动,OK.,然后就可以直接拷贝安装包或者连接调试了. 其它几个口都不行.

  3. 【六】Hystrix Dashboard

    除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard) , Hystrix会持续地记录所有通过 Hystrix发起的请求的执行信息,并以统计报表和图形 ...

  4. Hero Patterns - 聚合各种 SVG 背景纹理素材的网站

    Hero Patterns 是一个聚合了各种 SVG 背景纹理素材的网站,提供的多样的素材可以给你的网站带去特色. SVG 是一种分辨率无关的图形(矢量图形).这意味着它在任何类型的屏幕都不会遭受任何 ...

  5. 十三、u-boot 调试-- NOR FLASH 支持

    13.1 问题现象 在烧写进去的u-boot 中 Flash 并没有显示实际大小,需要进行修改. 13.2 问题定位过程 13.2.1 关键字搜索 Flash: 此关键字在 Board_r.c (co ...

  6. Nmap扫描基础常用命令(包含进阶使用)

    Nmap扫描常用命令  - Nmap scans common commands 1.扫描单个目标 nmap ip 如:nmap 192.168.0.101 2.扫描多个目标 nmap ip1 ip2 ...

  7. 第25月第9天 tf_tang_poems kaggle

    1.neural-style https://github.com/anishathalye/neural-style wget http://www.vlfeat.org/matconvnet/mo ...

  8. Windows下开启composer镜像服务来安装yii

    网上关于使用composer的安装教程挺多的,但是作为新手的我,觉得好凌乱,不断尝试后,终于安装好了.最后总结出,用开启composer的镜像服务来安装yii是最好的啦,当然,归档文件的做法有利有弊就 ...

  9. 深刻了解jQuery对象和普通DOM对象的区别

    深刻了解jQuery对象和普通DOM对象的区别.互相转化见Q1 Q1,js的写法:document.getElementById('save').disabled=true; 在jquery中我是这样 ...

  10. 技巧性极强的strings命令

    打印文件中的可打印字符串(print the strings of printable characters in files).常用来在二进制文件中查找字符串,与grep配合使用.strings命令 ...