Skip to content

Getting the current row number from MySQL

April 22, 2009

This morning I found a pretty slick way to pull the row number of a query (like Coldfusion’s #currentrow#) directly from MySQL.

select lookupID, lookupValue, lookupType, @rownum:=@rownum+1 as rownumber
from lookup, (SELECT @rownum:=0) r

This will return the lookupID,  lookupValue,  lookupType, and a number for each row, beginning with 1.

All the credit goes to this blog post, but I feel it’s always good to propagate tips like this.

Advertisement
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.