• laravel sql复杂语句,原生写法----连表分组


    ###

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

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

    ###

    原生的sql:

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

    获得测试数据结果:

    转换成laravel 的写法:

     1     $point_logs =  DB::table('point_logs')
     2                         ->whereIn('source_type', ['layer', 'manager', 'chief'])
     3                         ->whereBetween('is_verify', [1, 2]);
     4                $report_infos =  DB::table(DB::raw("({$point_logs->toSql()}) as p"))
     5                         ->mergeBindings($point_logs)
     6                         ->leftJoin('users', 'users.id', '=', 'p.user_id')
     7                         ->select(
     8                             'users.realname',
     9                             'users.phone',
    10                             'users.user_code',
    11                             DB::raw('sum(points) as sum_points'),
    12                             'source_type',
    13                             DB::raw('count(source_type) as number'),
    14                             'p.is_verify'
    15                             )
    16                         ->groupBy('user_id')
    17                         ->groupBy('source_type')
    18                         ->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 当元格合并处理

    单元格行合并方法:

     1   table_rowspan("#assign_table", 1);
     2       table_rowspan("#assign_table", 2);
     3       table_rowspan("#assign_table", 3);
     4         //函数说明:合并指定表格(表格id为table_id)指定列(列数为table_colnum)的相同文本的相邻单元格
     5         //参数说明:table_id 为需要进行合并单元格的表格的id。如在HTMl中指定表格 id="table1" ,此参数应为 #table1
     6         //参数说明:table_colnum 为需要合并单元格的所在列。为数字,从最左边第一列为1开始算起。
     7         function table_rowspan(table_id, table_colnum) {
     8             table_firsttd = "";
     9             table_currenttd = "";
    10             table_SpanNum = 0;
    11             colnum_Obj = $(table_id + " tr td:nth-child(" + table_colnum + ")");
    12             colnum_Obj.each(function (i) {
    13                 if (i == 0) {
    14                     table_firsttd = $(this);
    15                     table_SpanNum = 1;
    16                 } else {
    17                     table_currenttd = $(this);
    18                     if (table_firsttd.text() == table_currenttd.text()) {
    19                         table_SpanNum++;
    20                         table_currenttd.hide(); //remove();
    21                         table_firsttd.attr("rowSpan", table_SpanNum);
    22                     } else {
    23                         table_firsttd = $(this);
    24                         table_SpanNum = 1;
    25                     }
    26                 }
    27             });
    28         }

    此外还有列合并的方法:

     1  //函数说明:合并指定表格(表格id为table_id)指定行(行数为table_rownum)的相同文本的相邻单元格
     2         //参数说明:table_id 为需要进行合并单元格的表格id。如在HTMl中指定表格 id="table1" ,此参数应为 #table1
     3         //参数说明:table_rownum 为需要合并单元格的所在行。其参数形式请参考jQuery中nth-child的参数。
     4         //          如果为数字,则从最左边第一行为1开始算起。
     5         //          "even" 表示偶数行
     6         //          "odd" 表示奇数行
     7         //          "3n+1" 表示的行数为1、4、7、10.......
     8         //参数说明:table_maxcolnum 为指定行中单元格对应的最大列数,列数大于这个数值的单元格将不进行比较合并。
     9         //          此参数可以为空,为空则指定行的所有单元格要进行比较合并。
    10         function table_colspan(table_id, table_rownum, table_maxcolnum) {
    11             if (table_maxcolnum == void 0) {
    12                 table_maxcolnum = 0;
    13             }
    14             table_firsttd = "";
    15             table_currenttd = "";
    16             table_SpanNum = 0;
    17             $(table_id + " tr:nth-child(" + table_rownum + ")").each(function (i) {
    18                 row_Obj = $(this).children();
    19                 row_Obj.each(function (i) {
    20                     if (i == 0) {
    21                         table_firsttd = $(this);
    22                         table_SpanNum = 1;
    23                     } else if ((table_maxcolnum > 0) && (i > table_maxcolnum)) {
    24                         return "";
    25                     } else {
    26                         table_currenttd = $(this);
    27                         if (table_firsttd.text() == table_currenttd.text()) {
    28                             table_SpanNum++;
    29                             table_currenttd.hide(); //remove();
    30                             table_firsttd.attr("colSpan", table_SpanNum);
    31                         } else {
    32                             table_firsttd = $(this);
    33                             table_SpanNum = 1;
    34                         }
    35                    }
    36                 });
    37             });
    38         }
  • 相关阅读:
    记一次cdh6.3.2版本spark写入phoniex的错误:Incompatible jars detected between client and server. Ensure that phoenix-[version]-server.jar is put on the classpath of HBase in every region server:
    kylin的除法函数的坑
    Exception:kylin构建cube, Cannot modify mapReduce.queue.name at runtime
    cdh版本 livy部署
    kylin-3.1.1-bin-hadoop3搭建,构建cube报的错误,Cannot modify dfs.replication at runtime. It is not in list of params that are allowed to be modified at runtime
    apache开源 国内镜像地址
    一次phoniex表查询报出 org.apache.hadoop.hbase.NotServingRegionException
    spark高级分析2的数据集地址
    题解 P4240【毒瘤之神的考验】
    题解 P4688/BZOJ4939 【[Ynoi2016] 掉进兔子洞】
  • 原文地址:https://www.cnblogs.com/smallyi/p/7200814.html
Copyright © 2020-2023  润新知