简体   繁体   English

Laravel 4查询生成器SQL语句

[英]Laravel 4 Query Builder SQL Statement

Good day, I have this SQL statement and I'm having a difficulty on how to convert this in Laravel Query. 美好的一天,我有这个SQL语句,并且在如何在Laravel查询中转换此语句时遇到了困难。

I read the docs in http://laravel.com/docs/4.2/queries and I'm confused. 我在http://laravel.com/docs/4.2/queries中阅读了文档,我很困惑。 Any help would do. 任何帮助都可以。

SELECT count(*) FROM `transactions` WHERE `borrower_id` = 2 
            AND `book_id` = 2 AND (
                (
                    `reservedDate` IS NOT NULL 
                    and `borrowedDate` IS NULL
                ) 
                OR (
                    `borrowedDate` IS NOT NULL 
                    AND `returnedDate` IS NULL
                )
            )

So I try to build this query by reading the documentations from Laravel. 因此,我尝试通过阅读Laravel的文档来构建此查询。 Still haven't tested it yet but hope it could give you the right direction. 尚未进行测试,但希望它能给您正确的方向。

Maybe next time, you should show your own query (it's ok if it's not correct). 也许下次,您应该显示自己的查询(如果不正确,可以)。 It's easier to get start from there. 从那里开始更容易。

$super_query = DB::table('transactions')->where('borrower_id' , '=' , 2)->where('book_id', '=' , 2)->where( function ( $query ) {
$query->where(function ($query1) {
    $query1->whereNotNull('reservedDate')->whereNull('borrowedDate')
)->orWhere(function ($query2) {
    $query2->whereNotNUll('borrowedDate')->whereNull('returnedDate')
})  
})->count();
    DB::table('transactions')
  ->Where('borrower_id', '=', 2)
  ->Where('book_id', '=', 2)
  ->where(function($query)
  {
    $query->where(function($query1) {
      $query1->whereNotNull('reservedDate')
             ->whereNull('borrowedDate');

    })
    ->orWhere(function($query2) {
      $query2->whereNotNull('borrowedDate')
             ->whereNull('returnedDate');
  });
})->count();

try this.. this will result into 试试这个..这将导致

select count(*) from `transactions` where `borrower_id` = 2 and `book_id` = 2 and ((`reservedDate` is not null and `borrowedDate` is null) or (`borrowedDate` is not null and `returnedDate` is null))

Try this: 尝试这个:

Eloquent 雄辩

Transactions::where('borrower_id', '=', 2)
    ->where('book_id', '=', 2)
    ->where(function($query)
    {
        $query->whereNotNull('reservedDate')
                ->whereNull('borrowedDate');
    })
    ->orWhere(function($query)
    {
        $query->whereNotNull('borrowedDate')
                ->whereNull('returnedDate');
    })
    ->count();
book::count()->where('borrower_id','=','2','book_id','=',2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM