¿Cómo puedo hacer en lenguaje SQL para mostrar los últimos 10 registros de una tabla?

Sólo quiero poder mostrar los últimos 10 registros de una tabla con un select.
Si sabes como hacerlo échame un cable.

1 respuesta

Respuesta
1
Para mostrar los N primeros registros vale con Usar la sentencia TOP. Esta funciona asi :
SELECT TOP [Numero de registros] ...
La documentacion del TOP la tienes aqui :
http://users.starpower.net/rjhalljr/Serve/AccSql/sql.html
Por si no la ves es esta...
Returning Groups Of Rows In Sequence
For web pages, you often want to return rows in groups of some number. For example, you may want to insert the first twenty five rows into the first page, the next twenty five into the second page, and so on. If you can't or don't want to do this in the client applications, the following SQL statements will do the job. This approach assumes that the rows in the table are ordered on one column (fldOrder).
SELECT TOP 25 * FROM tbl;
Now it gets a little tricky. To get the next group of twenty five rows, start by selecting the first fifty.
SELECT TOP 50 * FROM tbl;
Now use the query above as a subquery in a query that pulls the bottom twenty five from the selected fifty.
SELECT TOP 25 *
FROM (
SELECT TOP 50 * FROM tbl
)
ORDER BY fldOrder DESC;
That gets the correct twenty five rows, but in the wrong order. The last step is to put them back in the proper order.
SELECT *
FROM (
SELECT TOP 25 *
FROM (
SELECT TOP 50 * FROM tbl
)
ORDER BY fldOrder DESC
)
ORDER BY fldOrder;
The SQL statement above will run, but when you close the query and then reopen it in SQL View, you'll see that Access has changed the statement. Don't worry about it. Either version works.
Es para access2000 desde ASP.
Gracias por tu ayuda.
¿En SQL para access o para Oracle?
Para oracle es con Rownum, pero los 10 últimos registros será segun que orden
Aclarame siguiendo el orden y si es para Oracle o para ACCESS. Para Access es con TOP. Aclaramelo y te envio ejemplos.
No te has leido la respuesta, si que viene :
Todo depende del orden en que los muestres.
Si quieres los 10 últimos seleccionados por descripcion,
Pones :
SELECT TOP 25 *
From TABLA
ORDER BY CampoQueOrdena ASC/DESC
(asc para ascendente Desc para descendente.)
Ten encuenta que una consulta select a una BBDD no tiene por defecto que devolverte los campos en ningun orden. Y decir los 10 últimos, no tiene sentido sin orden.
De hecho en el ejemplo que te he enviado :
Pone esta consulta
SELECT TOP 25 *
FROM (
SELECT TOP 50 * FROM tbl
)
ORDER BY fldOrder DESC;
Pero te dice que las obtiene en orden incorrecto. (Porque ha cambiado el orden de las top 50 para que le devuelva las 25 últimas las primeras.)
<<
That gets the correct twenty five rows, but in the wrong order. The last step is to put them back in the proper order.
SELECT *
FROM (
SELECT TOP 25 *
FROM (
SELECT TOP 50 * FROM tbl
)
ORDER BY fldOrder DESC
)
ORDER BY fldOrder;
>>
Te aseguro que me leo las preguntas. Por favor, haz lo mismo con las respuestas. No me dedico a contestar lo que no se me pide.
Gracias por tu ayuda pero lo que quiero mostrar son los DIEZ ÚLTIMOS y no los primeros, será algo parecido, si lo puedes encontrar me lo envias..
Gracias de nuevo.
Un saludo.
Perdona, pero mi inglés no es muy bueno y no entendí bien la respuesta, aunque si que la leí,
Muuchas Gracias por tu estupenda respuesta.
Un saludo.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas