在Laravel`DB :: Raw`上运行mysql查询

时间:2018-03-16 10:07:02

标签: php mysql laravel-5.6

我有一个MYSQL查询,我需要在Laravel 5.6查询构建器上运行。我的查询是

SELECT paper_id,user_id,COUNT(payments.user_id),users.district
FROM payments
LEFT JOIN users ON payments.user_id = users.id
WHERE payments.paper_id=3
GROUP BY users.district HAVING COUNT(payments.user_id)>=0;

我尝试使用此代码

在Laravel DB::Raw上运行此功能
$data=DB::Raw('SELECT paper_id,user_id,COUNT(payments.user_id),users.district
                    FROM payments
                    LEFT JOIN users ON payments.user_id = users.id
                    WHERE payments.paper_id='.$paper_id.'
                    GROUP BY users.district HAVING 
                    COUNT(payments.user_id)>=0 
                    ');

以及通过此代码

$data2=DB::table('payments')
                    ->leftJoin('users','payments.user_id','users.id')
                    ->select('paper_id','user_id','users.district',
                      DB::Raw('COUNT(payments.user_id)'))
                    ->where('payments.paper_id',$paper_id)
                    ->groupBy('users.district')
                    ->select(DB::Raw('HAVING COUNT(payments.user_id)>=0'))
                    ->get();

我遇到$data(第一次查询)查询时出错

[{}] 

我得到一个空的回复

我遇到`$ data2(第二次查询)查询错误

  

SQLSTATE [42000]:语法错误或访问冲突:1064您有错误       在你的SQL语法中;检查与MariaDB服务器对应的手册       用于在'HAVING COUNT(payments.user_id)> = 0附近使用的正确语法的版本       付款在第1行留下加入用户付款(SQL:选择HAVING       来自付款的COUNT(payments.user_id)> = 0加入用户       payments.user_id = users.id其中payments.paper_id = 1 group by       users.district)

但我得到SQL语法错误有人可以告诉我如何将SQL转换为Laravel查询构建器格式。

2 个答案:

答案 0 :(得分:0)

试试这个:

使用Raw查询

   $data = DB::select('SELECT paper_id,user_id,COUNT(payments.user_id),users.district
            FROM payments
            LEFT JOIN users ON payments.user_id = users.id
            WHERE payments.paper_id='.$paper_id.'
            GROUP BY users.district HAVING 
            COUNT(payments.user_id)>=0 
            ');

OR

        $data2= DB::table('payments')->select([
            'payments.paper_id','payments.user_id','users.district',
            DB::Raw('COUNT(payments.user_id)')
            ])
            ->leftJoin('users','payments.user_id','users.id')
            ->where('payments.paper_id',$paper_id)
            ->havingRaw("COUNT(payments.user_id)>=0")
            ->groupBy('users.district')
            ->get();

我认为select函数应该只在查询中使用一次,不需要添加多个时间。在添加任何questin总是搜索您的问题之前。希望它能起作用..

为您的错误

  

SQLSTATE [42000]:语法错误或访问冲突:1055

在此文件中 vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php 在laravel的这个位置替换此文件laravel5.4 PDO::ATTR_EMULATE_PREPARES => true,

答案 1 :(得分:0)

运行原始SQL查询

$data = DB::select('SELECT paper_id,user_id,COUNT(payments.user_id),users.district
                FROM payments
                LEFT JOIN users ON payments.user_id = users.id
                WHERE payments.paper_id= ?
                GROUP BY users.district HAVING 
                COUNT(payments.user_id)>=0', [$paper_id]);

参数绑定可防止SQL注入。

  

https://laravel.com/docs/5.6/database#running-queries

groupBy / have

->select(DB::Raw('HAVING COUNT(payments.user_id)>=0'))
=>
->having(DB::Raw('COUNT(payments.user_id)'), '>=', 0)
  

https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset