Sql last ids

1

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

    
asked by anonymous 03.07.2015 / 22:51

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 />";
}
    
03.07.2015 / 22:58