2017-10-18 73 views
0

在我的數據庫creadted_at中的數據爲2017-11-07 18:58:16,2017-11-07 19:58:16。我嘗試使用WhereBetween的日期2017年11月7日搜索數據 我不知道我怎樣才能把Like % %到我的查詢如何在sql查詢中使用WhereBetween日期與%LIKE%

'like', '%'.2017-11-07.'%' 

->WhereBetween('created_at', ['2017-11-07', '2017-12-07']) 

這裏是我的全部控制器

$b = DB::table("v_dealer_sell_time") 
      ->select([ 
        'product_name', 
        DB::raw('COALESCE(SUM(total_price), 0) AS total_price'), 
        DB::raw('COALESCE(SUM(total_product), 0) AS total_product') 
        ]) 
      ->WhereBetween('created_at', ['2017-11-07', '2017-11-07']) 
      ->groupBy('product_name') 
      ->get(); 
+0

你希望在同一天創建的數據2017-11-07 – Maraboc

+0

Opps我更新了2017-01-07和2017-12-07 – user8663822

+0

laravel的版本是什麼? – Maraboc

回答

1

如果你想創建的人在同一個給定的日期,你可以使用whereDate這樣但由於5.3(Documentation):

$b = DB::table("v_dealer_sell_time") 
      ->select([ 
        'product_name', 
        DB::raw('COALESCE(SUM(total_price), 0) AS total_price'), 
        DB::raw('COALESCE(SUM(total_product), 0) AS total_product') 
        ]) 
      ->WhereDate('created_at', '=', $date) 
      ->groupBy('product_name') 
      ->get(); 

如果從兩個日期之間要使用它whereBetween像這樣的:

$b = DB::table("v_dealer_sell_time") 
      ->select([ 
        'product_name', 
        DB::raw('COALESCE(SUM(total_price), 0) AS total_price'), 
        DB::raw('COALESCE(SUM(total_product), 0) AS total_product') 
        ]) 
      ->WhereBetween('created_at', [Carbon::parse('2017-11-07')->startOfDay(), Carbon::parse('2017-12-07')->endOfDay()]) 
      ->groupBy('product_name') 
      ->get(); 

PS:不要忘了在你的控制器的頂部添加use Carbon\Carbon;

+0

Opps我更新之間2017-11-07和2017-12-07 – user8663822

+0

檢查更新;) – Maraboc

+0

謝謝先生你總是幫我=]] – user8663822

1

使用whereDate()

$date = Carbon::parse($date)->toDateString(); 
.... 
->whereDate('created_at', $date) 

或者whereBetween()

$date = Carbon::parse($date); 
$from = $date->copy()->startOfDay(); 
$to = $date->copy()->endOfDay(); 
.... 
->whereBetween('created_at', [$from, $to]) 
+1

從來沒有嘗試碳之前謝謝生病嘗試 – user8663822

1

如果涉及到搜索功能這個日期搜索,那麼你必須做這樣的事情


代碼

更改爲

->WhereBetween('created_at', ['2017-11-07', '2017-11-07']) 

->WhereBetween('created_at', ['2017-11-07 00:00:00', '2017-11-07 23:59:59']) 

爲什麼?

2017-11-07 00:00:00 - 當天
2017-11-07 23:59:59的開始 - 一天結束


如果使用tosql()並檢查您的查詢有這樣的事情

.`created_at` between ? and ? " 
+0

謝謝先生。這是我尋找的 – user8663822