2011-04-15 70 views

回答

6
SELECT * FROM table WHERE FROM_UNIXTIME(timestamp, '%Y') = 2011; 
1
SELECT * FROM t WHERE YEAR(`timestamp`) = 2011 
+1

如果將year()函數應用於unix_timestamp,則返回null – 2011-04-15 12:42:33

+0

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html – trickwallett 2011-04-15 12:44:54

+1

這不起作用 – 2011-04-15 12:50:12

1
select * from table where from_unixtime(field,'%Y') = 2011 
1

嘗試這樣的查詢,其中myTimestamp是在它

SELECT * FROM myTable WHERE FROM_UNIXTIME(myTimestamp, '%Y') = '2011' 
2

時間戳列最簡單的方法已經被在答案的其餘部分從建議(直接查詢)

你也可以這樣做

$year_start = strtotime('2011-01-01'); 
$year_end = strtotime('2011-12-31 23:59:59.000'); 

$sql = "SELECT * FROM your_table 
     WHERE Timestamp>=$year_start AND Timestamp<=$year_end"; 
$res = mysql_query($sql); 

功能strtotime()取決於時區設置。