Hi, I know that SQL Server
something query
like this is done to take the LAST record NOT THE FIRST
SELECT TOP 1 *
FROM Tabla
ORDER by ID DESC
So it takes me the last record of the MSSQL table, that is SQL server 2008 R2 but in MySQL
place this and it takes me the first record. What should I do or what am I doing wrong? Read the beginning since they downvoted me after doing a little more research
SELECT *
FROM Tabla
ORDER by ID DESC
LIMIT 1
MAKING IT CLEAR that I just understood this, and I understood it after making this Post, that the equivalent of a
TOP 1
of SQL server is
MIN(exprecion)
and the last record is
MAX(exprecion)
Of course it is with your condition.
It's not clear what you've done wrong. Because the 2 statements are exactly equivalent and work perfectly fine.
TOP 1
in SQLServer:Result:
Demo
LIMIT 1
with mysqlResult:
Demo
As you can see, both queries work fine and return the same result.
On the other hand, the query you put as the answer to your question:
...is not valid at all, it doesn't even run, as you can see here:Demo
Maybe you meant:
... but although at least now it runs, this query can give you completely wrong results, as you can see in the following demo:
Result:
Demo
As I see it, you are taking the descending order and that would take the last record entered, taking into account that the id is auto-incremental. The query would look something like this:
SELECT * FROM Tabla ORDER by ID ASC LIMIT 1
This would bring you the oldest record. But keep in mind how the ID field grows
A beautifully flowery alternative:
The main virtue of this query is that if, for example, two books have the same minimum price, both records are displayed. On the other hand, "LIMIT 1" only presents us with one result.