mysql时间排序,如何在mysql中按日期排序

my column data is 1-jul,16-jul,5-jul,7-jul,15-jul,28-jul,10-jul i want this order by date

1-jul,5-jul,7-jul,10-jul,15-jul,28-jul

What I have tried:

select date from table order by date

解决方案Don't store it like that: never store dates a strings, it always gives problems later.

Instead, store it as DATETIME values and as individual rows in a table, not as comma separated values, with a foreign key back to the other table row they relate to. Generating a stored CSV list from that is almost trivial, where sorting by date a string based list of CSV data is possible but really pretty complicated. And it will cause other problems later as well, which are as complicated to fix.

Change your DB, and always store data in appropriate types.

Quote:How to order by date in mysqlAs you can see, it is a bad idea to store dates in common human form if you want to do something bigger than just read that date as is.

So the advice is never do it that way.

Date is a complicated data type and the computer must know that it is a date to be able to handle it.

Most languages have a DateTime data type because it is a very common data with very specific operations.

If you need to store a date as a string, the most practical format is "yyyymmdd", which gives "20160818" for today. this mostly what DateTime is doing internally.

For your problem, since the dates are stored in a bad form, you have to create a program to deal with them. Your program will read a string date split it in components and rebuild a string that will match this format "yyyymmdd" just to be able to sort the data. If you need to count days between dates, you have to do another program.

Transforming the string to DateTime type would simplify things.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值