Solved

MYSQL Record Number

Posted on 2016-08-31
3
86 Views
Last Modified: 2016-09-19
I want to be able to get the record number within my query.

I have a simple table
ID  NAME
11  steve
82  bob
32  peter


I can get the rank number of the query like this:

SELECT *, @rownum := @rownum + 1 AS rank
FROM table, (SELECT @rownum := 0) r
ORDER BY id

returns:
ID  NAME rank
11  steve  1
82  bob    2
32  peter  3



BUT if I run a query like this:

SELECT *, @rownum := @rownum + 1 AS rank
FROM table, (SELECT @rownum := 0) r
WHERE name = "bob"
ORDER BY id

Its returns:
ID  NAME rank
82  bob    1



But I want it to still return rank = 2.... the position in the db.


Can anyone help.

Steve
0
Comment
Question by:Steve Tinsley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41777662
try this?
select * from
(
SELECT *, @rownum := @rownum + 1 AS rank
FROM Table1, (SELECT @rownum := 0) r
) r
where name = 'bob'
ORDER BY id

Open in new window

1
 

Author Closing Comment

by:Steve Tinsley
ID: 41777673
Worked first time!! Thanks
0
 

Author Comment

by:Steve Tinsley
ID: 41805370
Hi Ryan,

I am suddenly getting issues with this query. It is giving me the number plus 1.
I dont completely understand how this works.

Any ideas??

Steve
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question