sql 查询指定日期时间记录sql语句与查询实例
首先创建一个表abc结构如下
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 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');
好,准备工作都作好了,我们现在要查询2010-04-22的数据
注:1271865600 =2010-04-23
1271952000 =2010-04-23
*/
//数据库教程连接
mysql教程_connect("localhost","root","root") or die('Database Server Uid or Password Error!');
mysql_select_db('cc') or die('data !');
mysql_query("SET NAMES 'gb2312'");//设置文档与数据库编码一致就OK,否则会出乱码
$currentTime = mktime(0,0,0,04,22,2010);
//利用mktime读取2010-04-22的时间截
$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 />";
}
/*
输出结果为
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
总结:
在数据库设计时特针对时间日期我们最好用mktime 时间戳,保存到数据以方便查询加减操作,用int(4)就足以保存当前时间了。
本站原创文章转载注明出处 www.111cn.net 中国WEB第一站