DANIEL
DANIEL

Reputation: 547

MySQL: How to get a record based on the position

I have a random number - like 10 - and I need to get the 10th register of a recordset.

I do a LOOP with count + 1 until 10.

but if the random number is really big, maybe I need to do a big loop too.

There is an easy method - in performance - to get this record?

NAME | GROUP
Alan    A
Paul    B
John    A
Frank   A

SELECT * FROM TABLE WHERE GROUP = A 

Randon number is "2" then:

I need to find the 2nd record:

NAME | GROUP
John     A

tks daniel

Upvotes: 0

Views: 29

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

Using LIMIT with an OFFSET you can get just the second row from the resultset

SELECT * FROM TABLE 
WHERE GROUP = 'A' 
LIMIT 1,1

Except that queries will return data in an order they find easy, so you may want to order the results as well

SELECT * FROM TABLE 
WHERE GROUP = 'A' 
ORDER BY NAME
LIMIT 1,1

Upvotes: 1

Related Questions