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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM