PHP:使用mysql获取数据库中的最后一行 [英] PHP: get the last row in database using mysql
问题描述
我在一个表中有三行.我正在尝试获取并显示表格的最后一行,相反,它始终显示中间行的结果.
I have three rows in a table. I'm trying to get and display the last row of my table and, instead, its always displaying the result of the middle row.
我曾尝试使用 "DESC"
或 "DESC LIMIT 1"
但仍然无法正常工作.
I have tried to used the "DESC"
or "DESC LIMIT 1"
but still its not working.
示例:
我的 tbldata
表中有 3 行.
I have a 3 rows in my table of tbldata
.
0:5
0:10
0:15
查询:
$result = mysql_query("SELECT fldBldgName, fldTimestamp,
MIN(fldTotalDuration)fldTotalDuration
FROM tbldata
WHERE fldNetname = '".$network."'
AND fldBldgName = '".$bldg[$i]."'
AND fldWeek = '".$week."'
AND fldMonth = '".$month."'
GROUP BY fldBldgName
ORDER BY fldBldgName,fldTimestamp, fldTotalDuration DESC");
我已经尝试了 ID to order
但仍然无法正常工作.我尝试了 timestamp
,但仍然无法正常工作.
I already tried also the ID to order
by but still its not working. I tried the timestamp
, but still its not working.
那个查询的结果总是"0:10"
,正确的输出应该是"0:15"
.
The result for that query is always the "0:10"
, the correct output should be "0:15"
.
但是如果我只有 2 行:
But then if I have 2 rows only:
0:5
0:10
结果是:0:10
这是正确的
所以,我也试过在phpmyadmin
中手动查询,但还是不显示最后一行.
And so, I also tried it to manually query it in phpmyadmin
, but it still does not display the last row.
推荐答案
假设您有一个 id
字段并设置了 AUTO_INCREMENT
.
Assuming you have an id
field with AUTO_INCREMENT
set.
那就试试这个
SELECT * FROM tbl ORDER BY id DESC LIMIT 1;
您对 MIN(fldTotalDuration)
和 GROUP BY
的使用完全混淆了您所追求的结果.
Your use of MIN(fldTotalDuration)
and GROUP BY
was totally confusing the result you were after.
这篇关于PHP:使用mysql获取数据库中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!