简体   繁体   English

如何使用php从mysql表中的同一字段中对morth和year值进行排序

[英]How to sort morth and year values in the same field from mysql table using php

I have a mysql table with month and year in same field as below. 我在下面的相同字段中具有月份和年份的mysql表。

+----------------+
|    duedate     |
+----------------+
|   Sept '12     | 
|   Oct '12      | 
|   Nov '12      | 
|   May'13       | 
|   Mar'13       | 
|   Mar '13      | 
|   Jan '13      | 
|   Feb '13      | 
|   Dec '12      | 
|   Aug '12      | 
|   Apr '13      | 
+----------------+

I want to retrieve this table by sorting month and year values in DESC order as below. 我想通过按DESC顺序对月份和年份值进行排序来检索此表,如下所示。

+------------+
|  duedate   |
+------------+
|   May'13   | 
|   Apr '13  | 
|   Mar '13  | 
|   Mar'13   | 
|   Feb '13  | 
|   Jan '13  | 
|   Dec '12  | 
|   Nov '12  |
|   Oct '12  |
|   Sept '12 |
|   Aug '12  | 
+------------+

Is it possible to sort as above.. I have tried below sql query but it sorts the data by DESC of year values only, month sorting is not working.. 是否可以如上所述进行排序。我尝试了以下sql查询,但仅按年份值的DESC排序了数据,月份排序不起作用。

"SELECT DISTINCT duedate FROM sample_table ORDER BY substr(duedate, -2) DESC, FIELD(duedate, 'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar', 'Feb', 'Jan')"

Please help.. 请帮忙..

You should store the date in a DATE type and not in a VARCHAR or a CHAR as it seems you are doing. 您应该将日期存储为DATE类型,而不是像看起来那样存储在VARCHARCHAR中。

In that case you could very simple retrieve the data in the order you want just using the ORDER BY in this way: 在这种情况下,您可以非常简单地按您想要的顺序以这种方式使用ORDER BY来检索数据:

select * from YOURTABLE order by date asc

If for any reason you can not change this, I would recommend you to add another column in your table (called read_date for example), and then just update your table with something like: 如果由于某种原因您无法更改此设置,我建议您在表中添加另一列(例如,称为read_date ),然后仅使用以下内容更新表:

update table YOURTABLE set real_date = date

That would add the correct value to the column and you would be able to work properly with dates. 这将为列添加正确的值,并且您将能够正确处理日期。

If you still don't want to do this, you can always cast the data and force it to be read as a date using the STR_TO_DATE function like this: 如果您仍然不想这样做,则可以始终使用STR_TO_DATE函数来强制转换数据并强制将其作为日期读取:

SELECT * FROM YOURTABLE ORDER BY STR_TO_DATE(date, '%b %y') DESC;

More information about Mysql date formats here . 有关Mysql日期格式的更多信息,请参见此处

It's not particularly easy to sort this data as it is seen by the database as text which will be sorted alphanumerically rather than in date order. 对这些数据进行排序并不是特别容易,因为数据库将其视为文本,将按字母数字顺序而不是按日期顺序进行排序。

The best solution would be to add month and year fields and store the values as integers then output the string value using PHP date() formatting. 最好的解决方案是添加月份和年份字段,并将值存储为整数,然后使用PHP date()格式输出字符串值。 You could then order by integer exactly how you want 然后,您可以按照所需的整数顺序进行排序

Alternatively you can use the FIELD keyword in your query to sort on, however this isn't the best way to do it 另外,您也可以在查询中使用FIELD关键字进行排序,但这并不是最好的方法

SELECT * FROM YOURTABLE ORDER BY FIELD(duedate, 'May 13', 'Jun 13', 'Jul 13', ....)

you can try like this- 你可以这样尝试

SELECT STR_TO_DATE(duedate,'%M \'%Y') as due  FROM demo ORDER BY due DESC

I am not sure but try with str_to_date in mysql 我不确定,但尝试在MySQL中使用str_to_date

See dem link: SQLFIDDLE 请参阅dem链接: SQLFIDDLE

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

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