Home  >  Article  >  Database  >  mysql records are output in reverse order according to the date field_MySQL

mysql records are output in reverse order according to the date field_MySQL

WBOY
WBOYOriginal
2016-08-20 08:48:111634browse

We know that output in reverse order is very simple

select * from table order by id desc

Just do this

Then the problem now is how to output the date field in reverse order

Here we use cast() to convert the specified field to the type we need

The following is the sql statement in the actual project

select * from water where phoneNumber=@phoneNumber order by cast(date as datetime) desc

We talk about learning without thinking, so let’s think about the deeper content.

After consulting the information, I learned that there are two ways to convert the type

1.cast() method

2.convert() method

Use format

1.cast(field name as data type)

Such as the above sql statement

cast(date as datetime)

2.convert(field name, data type)

Example: convert(da,datetime)

The above is the mysql record output based on the date field in reverse order introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support of the website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn