我有一个表:box(id自动增量,net_amount,created_at时间戳); 我需要在 php mysql 中创建一个查询来选择每月最后插入的记录。这样就可以得到每个月末的net_amount。 我正在尝试这个简单的查询:
select * from box
where box.created_at < 20055332516028
虽然我的表中的最大created_at是2017-10-14 10:42:30,但是当我使用给定的查询时没有记录,我需要增加数量才能获取记录!!!
最佳答案
20055332516028
不是 Unix 时间戳。您需要获取上个月月底的时间戳,如下所示:
$date = new DateTime();
$date->setDate($date->format('Y'),$date->format('m'),1);
$date->setTime(0,0,0);
$date->sub(new DateInterval('PT1S'));
$endOfMonth = $date->getTimestamp();
然后在查询中使用它:
select * from box where box.created_at < unix_timestamp(?) order by box.created_at desc limit 1
关于mysql - php - mysql 选择两个日期之间最后插入的记录(每月),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47364490/