Sql last ids

Asked

Viewed 29 times

1

How do I return the last 5 id with sql? NAME ID 1 John 2 William 3 Alberto 4 Alexander 5 Michael 6 Luke

And you will return: 2 William 3 Alberto 4 Alexander 5 Michael 6 Luke

  • use the limit(0,5) and order by desc that should solve

1 answer

1


Try:

SELECT * FROM Usuarios
ORDER BY ID DESC
LIMIT 5

Using PDO:

$pdo = new PDO('... seus dados ...');
$consulta = $pdo->query("SELECT id, nome FROM Usuarios ORDER BY ID DESC LIMIT 5");

while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
    echo "Nome: {$linha['nome']} - ID: {$linha['id']}<br />";
}
  • And how do I do this in PDO? Returned username

  • @Updated misakie, take a look

  • Solved! Thank you Maicon Carraro

Browser other questions tagged

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