這是一個查詢獲取過去一個月的所有行。抓取所有行,但在sql最後一個月
$time = time() - 9676800;
$q = $this->db->query("
select id
from ipAddress
where date > {$time}
");
但是我該如何調整這個查詢來獲取所有行的BESIDES過去一個月?基本上,我希望最終刪除超過1個月
這是一個查詢獲取過去一個月的所有行。抓取所有行,但在sql最後一個月
$time = time() - 9676800;
$q = $this->db->query("
select id
from ipAddress
where date > {$time}
");
但是我該如何調整這個查詢來獲取所有行的BESIDES過去一個月?基本上,我希望最終刪除超過1個月
只要你的方法計算「最後的/過去的一個月」滿足你,那麼它的簡單的所有行:
where date <= {$time}
您可以使用DATE_FORMAT ,date_sub函數獲取上個月的日期。 在這裏找到答案: mysql last month date statement 這裏 MySQL Query to calculate the Previous Month
你可以像這樣
$time = strtotime('-1 Month');
$q = $this->db->query("
select id
from ipAddress
where `date` <= {$time}
");
但如果date
就像2013-02-27 22:16:38
或2013-02-27
一個TIMESTAMP,DATE或DATETIME字符串,那麼你需要像
$time = date('Y-m-d H:i:s', strtotime('-1 Month'));
$q = $this->db->query("
select id
from ipAddress
where `date` <= '{$time}'
");
或純粹在SQL中
select id
from ipAddress
where `date` <= DATE_SUB(NOW(), INTERVAL 1 MONTH)
如果我沒記錯,date
是一個保留的mysql字,所以在你的sql中使用反引號。
這給了我與我的例子相同的結果。我的例子獲取過去一個月的所有行。我希望所有行在過去的一個月內都是BESIDES。基本上我想最終刪除1個月以上的所有行 – bghouse 2013-02-28 04:07:50
不,它不應該。看到這個SQL小提琴的證明:http://www.sqlfiddle.com/#!2/e556c/3 – 2013-02-28 21:51:15
您使用的平臺是? MySQL,SQL Server等 – Kermit 2013-02-28 03:17:32
im sorry ... mysql – bghouse 2013-02-28 04:08:32
您的要求非常模糊。請改善它們。 – ErikE 2013-03-01 05:45:11