简体   繁体   English

php-mysql选择两个日期之间的最后插入记录(每个月)

[英]php - mysql select last inserted record between two dates (every month)

I have a table: box (id autoincrement, net_amount, created_at timestamp); 我有一个表:框(id自动增量,net_amount,created_at时间戳); I need to create a query in php mysql to select the last inserted record every month. 我需要在php mysql中创建查询以选择每月插入的最后一条记录。 Thus to get the net_amount at the end of every month. 因此,在每个月末获得net_amount。 I am trying this simple query: 我正在尝试这个简单的查询:

select * from box
 where box.created_at < 20055332516028

While the max created_at in my table is 2017-10-14 10:42:30, there is no records when I use the given query, I need to increase the number to get the records!!! 虽然我表中的最大created_at是2017-10-14 10:42:30,但是当我使用给定查询时没有记录,我需要增加数量来获取记录!!!

if i understand your problem your looking for this:- 如果我了解您的问题,您正在寻找以下内容:

SELECT b1.*
FROM box b1 LEFT JOIN box b2
ON (b1.name = b2.name AND b1.id < b2.id)
WHERE b1.created_at < PUT YOUR DATE  and b2.id IS NULL;

Hope it helps 希望能帮助到你

20055332516028 is not a Unix timestamp. 20055332516028不是Unix时间戳。 You need to get timestamp of the end of the previous month, something like this: 您需要获取上个月底的时间戳,如下所示:

$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();

and then use it in a query: 然后在查询中使用它:

select * from box where box.created_at < unix_timestamp(?) order by box.created_at desc limit 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM