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.bKjia.c0m 中國WEB第一站