简体   繁体   English

获取最近日期的最后一条记录的问题 - PHP

[英]Issue getting last record with most recent date - PHP

As the title says I have this issue trying to get that record, my query works directly in PHPMyAdmin but when I use it in the code with PHP it gets the second last record正如标题所说,我在尝试获取该记录时遇到了这个问题,我的查询直接在 PHPMyAdmin 中工作,但是当我在 PHP 代码中使用它时,它会获取倒数第二条记录

My second last record has this values:我的倒数第二条记录具有以下值:

id_record = 17292 id_user = 6 my_date = 2016-12-31 11:09:08 id_record = 17292 id_user = 6 my_date = 2016-12-31 11:09:08

My last record:最后的记录:

id_record = 17618 id_user = 6 my_date = 2017-01-05 13:37:21 id_record = 17618 id_user = 6 my_date = 2017-01-05 13:37:21

This is the query I'm using to achieve this这是我用来实现此目的的查询

SELECT id_record FROM my_table WHERE my_date = (SELECT MAX(my_date) FROM my_table WHERE id_user = 6 ORDER BY id_record DESC LIMIT 1)

When I use this query directly in PHPMyAdmin it works fine it shows the record with id 17618 but in the code in PHP it works but it gets the second last record ( 17292 ), I have no idea why is happening this, do you have some idea?, I hope you can help me, thanks.当我直接在 PHPMyAdmin 中使用此查询时,它工作正常,它显示 ID 为17618的记录,但在 PHP 代码中它工作正常,但它获取倒数第二个记录( 17292 ),我不知道为什么会发生这种情况,你有吗想法?,我希望你能帮助我,谢谢。

This is the PHP code这是PHP代码

$last_record = mysql_query("SELECT id_record FROM my_table WHERE my_date = (SELECT MAX(my_date) FROM my_table WHERE id_user = 6 ORDER BY id_record DESC LIMIT 1)") or die (mysql_error());
$last_record = mysql_fetch_assoc($last_record);
$last_record = $last_record['id_record'];

Why you use a subquery ?为什么要使用子查询? you can directly select the last record like this:您可以像这样直接选择最后一条记录:

SELECT id_record 
FROM my_table 
WHERE id_user = 6 
ORDER BY id_record DESC
LIMIT 1;

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

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