MySQL Date time comparison simple example

Source: Internet
Author: User
The code is as follows Copy Code

SELECT * from TB where c> date_format (' 2007-07-06 ', '%y%m%d ') and C <= date_format (' 2007-07-09 ', '%y%m%d ');

SELECT * from TB where c> date (' 2007-07-07 ') and c< date (' 2007-07-09 ')


Statday is a Date data field of type 2006031001

The code is as follows Copy Code

SELECT * from Loginstat where statday> date_format (Curdate ()-1, '%y%m%d ') and Statday >= Date_format (curdate (), '%Y% m%d ') or: SELECT * from Loginstat where statday> date_format (Curdate ()-1, '%y%m%d%h ') and Statday >= ( Curdate (), '%y%m%d%h ');


Other uses:

The code is as follows Copy Code

SELECT * from Loginstat where Statday >= date_format (' $date 1 ', '%y%m%d%h ') and Statday < Date_format (' $date ', '%y%m%d %H ')


The time format for the MySQL database is 2008-12-28 18:08:08, and now you have to get a date time from one result set Rs. I first try to get the time with the Rs.getdate () method, the result is only month day, can not get hours, minutes and seconds. The Final Solution is:

The code is as follows Copy Code

Date Time1=new Date (Rs.gettimestamp ("Pub_time"). GetTime ());
SimpleDateFormat formattime = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String Pubtime=formatime.format (time1);

The obtained pubtime is a string and is converted in the SQL statement using MySQL's time function date_format (' Times ', ' format '):

The code is as follows Copy Code

String sqlstr= "SELECT * from Do_document where Pub_time<date_format (' +pubtime+" ', '%y-%m-%d%h:%i:%s ') Order by pub_ Time desc limit 0,1 ";

Then execute the SQL statement and find the record that satisfies the condition.

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.