laravel sql复杂语句,原生写法----连表分组
###
使用了临时表、又分组又连表的感觉好难写,使用拉 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复杂语句,原生写法----连表分组的更多相关文章
- SQL判断语句用法和多表查询
1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...
- Laravel SQL 查询语句集锦
1.从数据表中取得单一数据列 $user= DB::table('users')->where('name','John')->first(); 2.检索表中的所有行 复制代码代码如下: ...
- SQL Server语句创建数据库和表——并设置主外键关系
简单的创建数据库的 SQL 语句: use master go if exists(select * from sysdatabases where name='Test') begin select ...
- 关于同时查询父子名称的SQL查询语句的写法 id name parentId parentName .
parentid是1就是id为1的公司的子公司 如图 查询出所有的信息后 由于我要呈现的是parentName 不是parentId所以想问下SQL语句怎么写 谢谢啦~~:) 解法: SELECT s ...
- SQL基本语句:2.基本表
SQL基本表的增删改
- SQL删除语句同时向备份表插入数据
从这里摘抄下来的,觉得很不错,http://www.cnblogs.com/ljhdo/p/5792886.html#3503524 ,以后就用这种方式删除,再也不用担心删除错数据啦!!!
- Oracle和sql server中复制表结构和表数据的sql语句
在Oracle和sql server中,如何从一个已知的旧表,来复制新生成一个新的表,如果要复制旧表结构和表数据,对应的sql语句该如何写呢?刚好阿堂这两天用到了,就顺便把它收集汇总一下,供朋友们参考 ...
- ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法
ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法 有些情况下,SQL SERVER 2008r2中需要保存float,int类型的数据,当C 中的变量为double ...
- SQL Server中查询数据库及表的信息语句
/* -- 本文件主要是汇总了 Microsoft SQL Server 中有关数据库与表的相关信息查询语句. -- 下面的查询语句中一般给出两种查询方法, -- A方法访问系统表,适应于SQL 20 ...
随机推荐
- centos下问题:connect:network is unreachable
问题描述 弄了三台机器准备搭建一个集群,按照centos7系统,一台主节点安装桌面环境,两台计算节点.配置计算节点的时候,发现ping不通,出现connect:network is unreachab ...
- 自定义CRM系统
写在前面 之前在windows上写代码逻辑.搞前端等花了很长时间,跑通之后一直没往centos上部署, 昨天尝试部署下,结果发现静态文件找不到 =='' 由于写了2个组件: - arya model的 ...
- SpringBoot系列: 与Spring Rest服务交互数据
不管是单体应用还是微服务应用, 现在都流行Restful风格, 下图是一个比较典型的使用rest的应用架构, 该应用不仅使用database数据源, 而且用到了一个Weather微服务, 另一方面, ...
- Pre-shared key
Pre-shared key https://en.wikipedia.org/wiki/Pre-shared_key In cryptography, a pre-shared key (PSK) ...
- 高并发秒杀系统--mybatis整合技巧
mybatis实现DAO接口编码技巧 1.XML文件通过namespace命名空间关联接口类 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD ...
- namecheap域名设置Cloudflare为第三方DNS
待更……等我搞完了就来写总结
- oracle.sql.TIMESTAMP转为java.sql.TIMESTAMP的方法
/** * @reference oracle.sql.Datum.timestampValue(); * @return */ private Timestamp getOracleTimestam ...
- jq的error
error事件会在js发生错误或资源加载失败时触发.该事件主要用于window对象.<img>等元素. 此外,你可以为同一元素多次调用该函数,从而绑定多个事件处理函数.触发error事件时 ...
- Shiro入门 - 通过自定义Realm连数数据库进行授权
shiro-realm.ini [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Spring中的注 ...
- sublime text 3 左侧目录树中文文件夹显示方框问题解决
0 - 解决方法 打开Preferences->Settings 在弹出的Settings对话框中,加入"dpi_scale": 1.0 重新启动sublime text 3 ...