How do I return the last 5 id with sql? NAME ID 1 John 2 Guilherme 3 Alberto 4 Alexandre 5 Michael 6 Lucas
And it will return: 2 Guilherme 3 Alberto 4 Alexandre 5 Michael 6 Lucas
How do I return the last 5 id with sql? NAME ID 1 John 2 Guilherme 3 Alberto 4 Alexandre 5 Michael 6 Lucas
And it will return: 2 Guilherme 3 Alberto 4 Alexandre 5 Michael 6 Lucas
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 />";
}