Mysql query Timestamp and date conversion

Source: Internet
Author: User
Mysql query Timestamp and date conversion are used in the database. It is often necessary to query records by specified date to facilitate statistics. In the database, many of them store timestamps, some directly store dates, which may not be so easy to query. mysql provides two functions: from_unixtime (time_stamp)

Mysql query Timestamp and date conversion are used in the database. It is often necessary to query records by specified date to facilitate statistics. In the database, many of them store timestamps, some directly store dates, which may not be so easy to query. mysql provides two functions: from_unixtime (time_stamp)-converts a timestamp to a date unix_ti

Mysql query Timestamp and date conversion

In the use of the database, you often need to query records by specified date for statistics. In the database, many of them store timestamps,

Some directly store dates, which may not be so easy to query.

Mysql provides two functions:

From_unixtime (time_stamp)-> convert a timestamp to a date

Unix_timestamp (date)-> converts a specified date or date string to a timestamp


For example: from_unixtime (time_stamp)

select from_unixtime(1382544000);+---------------------------+| from_unixtime(1382544000) |+---------------------------+| 2013-10-24 00:00:00       |+---------------------------+

For example, unix_timestamp (date)

select unix_timestamp(date('2013-10-24'));+------------------------------------+| unix_timestamp(date('2013-10-24')) |+------------------------------------+|                         1382544000 |+------------------------------------+


If you want to query the order records for the current day:

 select count(*) from b_order Where  date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')

You can also do this:

select count(*) from b_order Where  create_time >= unix_timestamp('2013-10-24 00:00:00') and create_time <=  unix_timestamp('2013-10-24 23:59:59') ;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.