Show last results but with ASC sorting in PHP

Asked

Viewed 265 times

3

I would like to show the last 10 results found in a table, but with the ASC sort. I am using the following:

$listarResultados = $pdo->prepare("SELECT * FROM teste WHERE categoria = 'cateste' ORDER BY id ASC LIMIT 10");
$listarResultados ->execute();

Turns out, this select returns me the first 10 results, and I want to list the last 10 in ASC order. What’s the right way to do it?

1 answer

4


Use a sub-query:

SELECT *
FROM (SELECT * FROM teste WHERE categoria = 'cateste' ORDER BY id DESC LIMIT 10) S
ORDER BY id ASC

Browser other questions tagged

You are not signed in. Login or sign up in order to post.