Check last record of an sql table

0

Good luck, next ... I'm trying to get the last record from a table with an id

SELECT * FROM tabela WHERE id = ? ORDER BY id DESC LIMIT 1

Except that he is not returning the last record, he is traversing the whole table behind a record, can anyone tell me how do I do this? Thank you in advance!

    
asked by anonymous 09.01.2018 / 22:31

3 answers

1

this works fine but add a column by repeating the id that you can delete or use subquery

SELECT MAX(collummID),* FROM tabela
    
09.01.2018 / 22:52
1

The WHERE clause will restrict the search to the number that you put in ? . The correct query would be:

SELECT * FROM tabela ORDER BY id DESC LIMIT 1;
    
09.01.2018 / 22:41
0

The above answer is correct, when you set the id (or any field in the table), the query will only bring whatever has that specified value.

There are some alternatives to the proposed solution that you can see there in w3schools :

TOP:

SELECT TOP 1 * FROM tabela ORDER BY id;

ROWNUM:

SELECT * FROM tabela WHERE ROWNUM = 1;
    
09.01.2018 / 22:51