月份名称在mysql中排序

时间:2016-05-26 10:14:28

标签: php mysql laravel

亲爱的朋友,我需要帮助,

这是我的查询,

$customer = DB::table('customer');
$customer->join('customer_month_calender', 
   'customer_month_calender.customer_id', '=', 'customer.id','left');

$customer->select('customer.id','customer_month_calender.month');
//$customer->where(DATE_FORMAT(month,'%m')=date('m',strtotime('+1 month')));
$customer->orderBy('customer_month_calender.month', 'create_date');
$customer->groupBy('customer.id');

return $customer->get(); 

此查询返回

的值
April  
August  
December  
February  
January  
July  
June

但是,我希望按顺序列出月份名称

(即1月,2月,3月......等到最后一个是12月)。

请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:0)

您可以使用orderBy raw查询来编写这样的查询:

$customer = DB::table('customer');
$customer->join('customer_month_calender', 
   'customer_month_calender.customer_id', '=', 'customer.id','left');

$customer->select('customer.id','customer_month_calender.month');
//$customer->where(DATE_FORMAT(month,'%m')=date('m',strtotime('+1 month')));
$customer->orderBy(DB::raw('month(create_date) ASC'));
$customer->groupBy('customer.id');

return $customer->get(); 

答案 1 :(得分:0)

如果您使用的是this mysqli类,可以尝试使用

$customer->orderBy('MONTH(create_date)', 'ASC');

使用原始查询:

ORDER BY MONTH(create_date)

当然,您可以自定义以满足您的需求

答案 2 :(得分:0)

    $customer = DB::table('customer');
$customer->join('customer_month_calender', 
   'customer_month_calender.customer_id', '=', 'customer.id','left');

$customer->select('customer.id','customer_month_calender.month');
//$customer->where(DATE_FORMAT(month,'%m')=date('m',strtotime('+1 month')));
$customer->orderBy('customer_month_calender.month ASC');
$customer->groupBy('customer.id');

return $customer->get();