laravel查询构建器表达式,来自sql表达式

时间:2018-03-29 07:48:39

标签: laravel select count query-builder laravel-query-builder

a16s            
id  pic     
1   1.jpg       
2   2.jpg       
3   3.jpg       
4   4.jpg       

a16s_like           
id  p_id u_id   approve
1   1       2   0
2   1       1   1
3   1       5   1
4   1       6   1
5   1       7   0
6   2       2   0
7   2       3   0
8   2       1   1
9   4       4   0
10  4       3   1
11  4       2   1


SELECT 
    A.id, 
    A.PIC, 
    SUM(CASE WHEN  B.approve IS NULL THEN 0 ELSE 1 END) AS Ashowcunt,
    SUM(CASE WHEN  B.approve=0 THEN 1 ELSE 0 END) AS Nshow,
    SUM(CASE WHEN  B.approve=1 THEN 1 ELSE 0 END) AS Yshow,
    B.approve,
    SUM(CASE WHEN  B.approve=1 AND B.u_id=3 THEN 1 when B.approve=0 AND B.u_id=3 then 0 ELSE null END) AS U_id3show
FROM  a16s AS A
LEFT JOIN a16s_like  AS B ON A.ID = B.p_id 
GROUP BY A.id,A.pic

获取列表并在mysql 5.7上运行良好

当u_id = 2来执行select时,我得到

id pic    Ashowocunt  approve_0_count  approve_1_count     u_id2_approve   
1  1.jpg   5          2                3                   0
2  2.jpg   3          2                1                   0
3  3.jpg   0          0                0                   null
4. 4.jpg   3          0                3                   0

U_ID = 3

id pic    Ashowocunt  approve_0_count  approve_1_count     u_id3_approve   
1  1.jpg   0          0                0                   null
2  2.jpg   0          1                0                   0
3  3.jpg   0          0                0                   null
4. 4.jpg   1          0                1                   1   

当我将sql更改为laravel

$search_alls=
DB::select('A.id','A.route','B.approve')
    ->addSelect(DB::raw('SUM(CASE WHEN B.approve IS NULL THEN 0 ELSE 1 END) as Ashowcount'))
    ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 0 THEN 1 ELSE 0 END) as Nshow'))
    ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 THEN 1 ELSE 0 END) as Yshow'))
    ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 AND b.u_id = 2 then 1 
                                  when B.approve = 0 AND b.u_id = 2 then 0 ELSE null END) as U_idshow'))
    ->from('a16s as A')
    ->join('a16s_like as B', function($join) {
        $join->on('A.ID', '=', 'B.p_id');
        })
    ->groupBy('A.id')
    ->orderby('A.id', 'DESC')
    ->paginate(12);


    return View('comefo.results')
            ->with('search_alls', $search_alls)
            ->with('table',$table);

我收到了错误

Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_RECOVERABLE_ERROR) 类型错误:传递给Illuminate \ Database \ Connection的参数1 ::连接:: prepareBindings()必须是类型数组,给定字符串,在D:\ AppServ \ www \ product \ vendor \ laravel \ framework \ src \ Illuminate \ Database \中调用第665行的Connection.php

2 个答案:

答案 0 :(得分:1)

您必须明确创建一个查询:

DB::query()->select(...

答案 1 :(得分:1)

您将表名放在错误的位置。使用table方法。

echo DB::table('a16s as A')
     ->select('A.id','A.route','B.approve')
     ...
     ->orderby('A.id', 'DESC')->toSql();

显示正常的sql,如

  

选择AidArouteBapprove,SUM(仅适用于B.approve   IS NULL(0 ELSE 1 END)as Ashowcount,SUM(例如B.approve = 0   那么1节0结束)作为Nshow,SUM(例如,当B.approve = 1那么1 ELSE 0   END)作为Yshow,SUM(例如,当B.approve = 1并且b.u_id = 2然后是1时   B.approve = 0 AND b.u_id = 2然后0 ELSE null END)作为U_idshow来自   a16s A a16s_like B A ID B p_id = Aid   按A分组。idCtrl+K Ctrl+0排序。x = {x1, x2, x3, ..., xn} desc