Show Last 10 Records in a table.

dk01's picture

He has: 516 posts

Joined: Mar 2002

Ok I know how to show the last 10 records as long as they are showed in Descending order. Here is what I was using:

SELECT DebugID, DebugText FROM Debug ORDER BY DebugID DESC LIMIT 10'

DebugID is int(11), auto increment, primary key, not null
DebugText is varchar(255)

This works but it shows them in the order:

22 | Hello Hello Hello
21 | Hello Hello Hello
20 | Hello Hello Hello
19 | Hello Hello Hello
18 | Hello Hello Hello
17 | Hello Hello Hello
16 | Hello Hello Hello
15 | Hello Hello Hello
14 | Hello Hello Hello
13 | Hello Hello Hello
'

I instead want to show them in the following way:

13 | Hello Hello Hello
14 | Hello Hello Hello
15 | Hello Hello Hello
16 | Hello Hello Hello
17 | Hello Hello Hello
18 | Hello Hello Hello
19 | Hello Hello Hello
20 | Hello Hello Hello
21 | Hello Hello Hello
22 | Hello Hello Hello
'

I know this is easy but its pretty late and I am mind blanking. I want to do it all in one query too.

Thanks!

-dk

chrishirst's picture

He has: 379 posts

Joined: Apr 2005

ORDER BY column ASC  '

dk01's picture

He has: 516 posts

Joined: Mar 2002

All that does is get the first 10 records.

Looks like this:

1 | Hello Hello Hello
2 | Hello Hello Hello
3 | Hello Hello Hello
4 | Hello Hello Hello
5 | Hello Hello Hello
6 | Hello Hello Hello
7 | Hello Hello Hello
8 | Hello Hello Hello
9 | Hello Hello Hello
10 | Hello Hello Hello
'

Any other suggestions?

-dk

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Not sure what DB you're using, but if it allows nested SQL statements in a LIMIT statement, the following should work:

SELECT DebugID, DebugText FROM Debug ORDER BY DebugID ASC LIMIT (SELECT COUNT(*) FROM Debug)-10, 10

If it won't allow it (I don't think mySQL will), you'll have to execute the nested one, pull in the count, then execute the other.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

if it is only 10 items, try something like this:

<?

// already opened and did the sql query with
// resutls into $dbResult

$arDebug = array();

while ($thisRow = mysql_fetch_assoc($dbResult))
$arDebug[$thisRow['DebugID']] = $thisRow['DebugText'];

ksort($arDebug);
reset($arDebug);

// Close database connection if not needed further
// You now have a 10 element array with data in order

echo "<table><tr><th colspan=2>Debug Info</th></tr>\n";
foreach ($arDebug as $key => $value)
echo "<tr><td>$key</td><td>$value</td></tr>\n";
echo "</table>\n";

?>
'

dk01's picture

He has: 516 posts

Joined: Mar 2002

Yup sorry MySql is what I am using. Bummer.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

If you debugID's do not skip any numbers, from what I was reading in the manual, this should work if you are using mySQL 4.1 or above.
(sadly I'm at 4.0 something so I can't test it...)

Quote: SELECT DebugID, DebugText
FROM Debug
WHERE DebugID > (SELECT MAX(DebugID)-10 FROM Debug)
ORDER BY DebugID ASC

dk01's picture

He has: 516 posts

Joined: Mar 2002

Yeah I am 4.0 also. I am just using 2 queries. You'd think something simple like this wouldn't be a problem. Oh well!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.