SQL query specify date time record SQL statement and query instance

Source: Internet
Author: User
Tags bulk insert mysql tutorial

SQL query specify date time record SQL statement and query instance

First create a table ABC structure is as follows

CREATE TABLE ' cc '. ' Loup ' (
' ID ' INT (4) Not NULL auto_increment,
' DateTimes ' INT (4) NULL,
' IPAddress ' VARCHAR NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM

Then BULK INSERT data

INSERT into ' loup ' (' id ', ' datetimes ', ' IPAddress ') VALUES
(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 '),
(7, 1271952000, ' 192.168.0.1 '),
(8, 1271952000, ' 192.168.0.1 '),
(9, 1271952000, ' 192.168.0.1 '),
(10, 1271952000, ' 192.168.0.1 ');

All right, we're ready for the 2010-04-22 data.

Note: 1271865600 =2010-04-23
1271952000 =2010-04-23

*/

Database Tutorial Connections

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 in line with the database code OK, otherwise there will be garbled

$currentTime = Mktime (0,0,0,04,22,2010);

Using Mktime to read the 2010-04-22 time cut

$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/>";
}

/*
Output results are

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

Summarize:
When designing a database specifically for a time date we'd better use the mktime timestamp to save the data for easy query and subtraction operations, with INT (4) enough to save the current time.

This site original article reproduced annotated source www.111cn.net China web first stop

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.