How to select only 10 last rows in a table?

1,088 views
Skip to first unread message

Vladimir Makarov

unread,
Sep 12, 2012, 9:09:07 AM9/12/12
to web...@googlegroups.com
So, I need to select data from the table but the only ten last rows (with highest id).
I have already tried with select all records and then use while statement but I think there is another way.
Any ideas? Thanks!

Massimo Di Pierro

unread,
Sep 12, 2012, 9:12:51 AM9/12/12
to web...@googlegroups.com
if you have a SQL db then you can order them by reverse id

db(....).select(orderby=~db.tablename.id,limitby=(0,10))

otherwise you should have some timestamp field (as created_on in auth.signature) and use that field.

Annet

unread,
Sep 12, 2012, 12:29:04 PM9/12/12
to web...@googlegroups.com
Hi Vladimir,

Some time ago Anthony provided me with the following solution:

maxID=db(db.node).select(db.node.id.max()).first()['MAX(node.id)']
rows=db(db.node.id>=maxID-10).select(db.node.id,db.node.computedName,orderby=~db.node.id)


Kind regards,

Annet

Massimo Di Pierro

unread,
Sep 12, 2012, 12:35:40 PM9/12/12
to web...@googlegroups.com
Instead of this

maxID=db(db.node).select(db.node.id.max()).first()['MAX(node.id)']

I would do

maxID=db(db.node).select(db.node.id.max()).first()[db.node.id.max()]

to make sure it will continue work in the future. The former is implementation dependent.

Anthony

unread,
Sep 12, 2012, 1:09:39 PM9/12/12
to web...@googlegroups.com
On Wednesday, September 12, 2012 12:35:41 PM UTC-4, Massimo Di Pierro wrote:
Instead of this

maxID=db(db.node).select(db.node.id.max()).first()['MAX(node.id)']

I would do

maxID=db(db.node).select(db.node.id.max()).first()[db.node.id.max()]

Well, that's what I really recommended: https://groups.google.com/d/msg/web2py/VY71mF2cl-4/oNE00OduXQ0J. :-)
 

Niphlod

unread,
Sep 12, 2012, 2:36:37 PM9/12/12
to web...@googlegroups.com
ps: methods are NOT equivalent. They are if you have "continous" ids.
But, e.g., you remove some rows. You end up with 1,2,3,4,5,6,7,8,9,10,11,13,15,17,20.
Second method (i.e. calc max and go back by ten) leaves you with 20,17,15,13,11,10 (and takes two queries)
First method (i.e. orderby + limitby) correctly returns 20,17,15,13,11,10,9,8,7,6.

Anthony

unread,
Sep 12, 2012, 3:39:07 PM9/12/12
to web...@googlegroups.com
Yes, the "max" solution was originally for a different problem that only needed the single max value and involved only one query. In this case, the orderby/limitby solution is the way to go.

Anthony

Massimo Di Pierro

unread,
Sep 12, 2012, 4:03:40 PM9/12/12
to web...@googlegroups.com
On a second look.... this assumes that no record was deleted. It does not always select the last 10 records.


On Wednesday, 12 September 2012 11:29:04 UTC-5, Annet wrote:

Niphlod

unread,
Sep 12, 2012, 5:44:58 PM9/12/12
to web...@googlegroups.com
@anthony, massimo: I surely trust that you know what are you doing.
My reply was only a specification for "future references" as the title of the post could be found by some users and may come handy.

Anthony

unread,
Sep 12, 2012, 9:10:11 PM9/12/12
to web...@googlegroups.com
Yes, I'm glad you pointed it out.

Vladimir Makarov

unread,
Sep 13, 2012, 1:20:45 AM9/13/12
to web...@googlegroups.com
Thank you all for your hints!!!
The easiest way, of course, is to use orderby and limitby. It works fine.
But the MAX method is usefull too. I'll use it in my projects.
Reply all
Reply to author
Forward
0 new messages