在PHP中获取MySQL的最后7行

时间:2009-12-28 16:59:27

标签: php mysql

我正在尝试从用户输入记录的表中获取最后7条记录

这是我的问题:

$database->setQuery("SELECT * FROM #__mytable WHERE (user_id = '$uid')");
$dberr="";
if (!$database->query()) {
    $dberr = $database->getErrorMsg();
}

if(!$dberr==""){
   echo($dberr."<br>");
}else{
   $rows = $database->loadObjectList();

如何通过$行循环获取最后7行?

3 个答案:

答案 0 :(得分:5)

你没有:

SELECT * FROM ... WHERE ... ORDER BY user_id DESC LIMIT 7

答案 1 :(得分:3)

SELECT  *
FROM    #__mytable
WHERE  user_id = '$uid'
ORDER BY
       entered_date DESC
LIMIT 7

要按升序排列,请使用:

SELECT  *
FROM    (
        SELECT  *
        FROM    #__mytable
        WHERE  user_id = '$uid'
        ORDER BY
               entered_date DESC
        LIMIT 7
        ) q
ORDER BY
        entered_date

答案 2 :(得分:2)

SELECT * FROM #__mytable WHERE (user_id = '$uid') ORDER BY id DESC LIMIT 0,7