如何在MySQL&中订购月份名称PHP?

时间:2014-03-08 13:21:17

标签: php mysql sql

我有查询显示我的数据库:

    SELECT archieve, SUM(items_in) AS income, SUM(items_out+black_out+white_out) AS outcome, SUM((ball_out+black_out+white_out
    )-items_in) AS efficiency, SUM((items_in / ( ball_out + black_out + white_out))*100) AS percent 
    FROM items 
    GROUP BY archieve
ORDER BY DATE_FORMAT(archieve,'%m')

我的archieve表只是提交数据的月份和年份。例如:

Jan 2014, Feb 2014, Mar 2014

但在我的页面中显示的是:

Feb 2014, Jan 2014, Mar 2014

这是我的PHP代码,用于将数据从我的网页插入数据库:

$date_trans = date("d M Y");
$archieve   = date("M Y");
$items_in   = mysql_real_escape_string($_POST['items_in']);
$ball_out   = mysql_real_escape_string($_POST['ball_out']);
$black_out  = mysql_real_escape_string($_POST['black_out']);
$white_out  = mysql_real_escape_string($_POST['white_out']);
$ball   = mysql_real_escape_string($_POST['ball']);
$black  = mysql_real_escape_string($_POST['black']);
$white  = mysql_real_escape_string($_POST['white']);
$note   = mysql_real_escape_string($_POST['note']);

代码一切都很好,但如何按月订购呢?因为我尝试了MONTH(日期)或日期。(MONTH)它不起作用。在此先感谢;)

2 个答案:

答案 0 :(得分:1)

您需要先将字符串“archieve”转换为日期,然后按值排序。 试试这个:

ORDER BY STR_TO_DATE(archieve, '%M %Y')

答案 1 :(得分:0)

试试这个

   ORDER BY FIELD(archieve,'Jan','Feb','Mar',...)

或者

  ORDER BY FIELD(DATE_FORMAT(archieve,'%m'),'01','02','03',...)