SQL query a specified date and time record SQL statement and query an instance

Source: Internet
Author: User
Tags mysql tutorial

SQL query a specified date and time record SQL statement and query an instance

Create a table with the following abc structure:
 
Create table 'cc'. 'loup '(
'Id' INT (4) not null AUTO_INCREMENT,
'Datetimes 'INT (4) NULL,
'Ipaddress' VARCHAR (20) NULL,
Primary key ('id ')
) ENGINE = MYISAM
 
Insert data in batches
 
Insert into 'loup' ('id', 'datetimes ', 'ipaddress') VALUES
(1, 1271865600, '192. 0.0.1 '),
(2, 1271865600, '192. 0.0.1 '),
(3, 1271865600, '192. 0.0.1 '),
(4, 1271865600, '192. 0.0.1 '),
(5, 1271865600, '192. 0.0.1 '),
(6, 1271865600, '192. 0.0.1 '),
(7, 1271952000, '192. 168.0.1 '),
(8, 1271952000, '192. 168.0.1 '),
(9, 1271952000, '192. 168.0.1 '),
(10,127 1952000, '192. 168.0.1 ');
 
Well, we have all the preparations. Now we want to query the data from.
 
Note: 1271865600 = 2010-04-23
1271952000 = 2010-04-23
 
*/

// Database tutorial connection

Mysql tutorial _ connect ("localhost", "root", "root") or die ('database Server Uid or Password Error! ');
Mysql_select_db ('cc') or die ('data! ');
Mysql_query ("set names 'gb2312 '"); // SET the document to be consistent with the database encoding. Otherwise, garbled characters may occur.

$ CurrentTime = mktime );

// Use mktime to read the time cut from

$ SQL = "Select * from loup where datetimes = $ currentTime ";
$ Query = mysql_query ($ SQL );
While ($ rs = mysql_fetch_array ($ query ))
{
Echo $ rs [0], '|', $ rs [1], '|', $ rs [2], "<br/> ";
}

/*
The output result is

1 | 1271865600 | 127.0.0.1
2 | 1271865600 | 127.0.0.1
3 | 1271865600 | 127.0.0.1
4 | 1271865600 | 127.0.0.1
5 | 1271865600 | 127.0.0.1
6 | 1271865600 | 127.0.0.1

Summary:
During database design, we recommend that you use the mktime timestamp to save the data to facilitate addition and subtraction queries. int (4) is enough to save the current time.

Original Site Article Reprinted with the source www. bKjia. c0m China WEB First stop

Related Article

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.