2014-12-22 142 views

回答

0

使用MySQL Month功能

SELECT * FROM tbl_name WHERE MONTH(col_name) = 3 

在laravel:

$name = DB::table('tbl_name')->whereRaw('MONTH(col_name) = 3')->get(); 
+0

兄弟你好!它在PHP中工作。如何在laravel查詢 – user2983060

+0

SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'MONTH(bookingdate)'(SQL:select count(*)as'bookingreports'中的聚合,其中'userid' = 77和'MONTH(bookingdate)'= 9) – user2983060

+0

但是有一個列名爲bookingdate – user2983060

0

試試這個它會工作:

SELECT * FROM table_name WHERE MONTH('2014-10-13') = '10'; 

幼蟲:

$results = DB::select('select * from users where MONTH('2014-10-13') = ?', array(10)); 
+0

您能否告訴我這個laravel查詢 – user2983060

+0

@ user2983060,現在檢查。 –

0

讓我假設表名作爲用戶,並假設您使用雄辯,因爲您已將它標記在laravel下。

這是如何獲得在特定月份創建的用戶。

雄辯:

$month = 12;// for december 
$users = User::whereRaw('MONTH(created_at) = ?',[$month])->get(); 
return $users; 

查詢生成器:

$users = DB::table('users')->whereRaw('MONTH(created_at) = ?',[$month])->get(); 

,如果你也想獲得當月的結果的話,

雄辯:

$users = User::select(['*',DB::raw('MONTH(created_at) as month')]) 
           ->whereRaw('MONTH(created_at) = ?',[$month])->get(); 

查詢生成器:

$users = DB::table('users')->select(['*',DB::raw('MONTH(created_at) as month')]) 
          ->whereRaw('MONTH(created_at) = ?',[$month])->get(); 
相關問題