1

I have a table called "participants" that has 3 fields:
prt_id
prt_event_id
prt_participant_id

What I have is a select query with a where condition on event_id. The query returns let's say 20 rows (20 different participants). What I would like to do is to be able to figure out the row number for a given participant (prt_id).

SELECT * 
FROM participants
WHERE prt_id = someinteger
3
  • RowID is a feature of Oracle only: docs.oracle.com/cd/B19306_01/server.102/b14200/… Commented Mar 9, 2012 at 13:40
  • What do you need that for? The order of the rows is not the order you added the records, you know. Commented Mar 9, 2012 at 13:40
  • Of course... But I wanted to keep it simple for the post. Indeed I ORDER my select query based on a timestamp... Commented Mar 9, 2012 at 13:45

5 Answers 5

3

While you can't specifically find a row ID using MySQL, you could do something like the following:

$conn = new mysqli(/*dbinfo*/);
$res = $conn->query("SELECT prt_id FROM participants");

$rowids = array(); $currid = 1;
while ($row = $res->fetch_object()) { // this is using the mysqli library
  $rowids[$row->prt_id] = $currid;
  $currid++;
}

This would give you an array of ids associated with prt_id.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello Baez. I was looking for another solution than a while loop but as I see there is no such things as rowId or something like that. thks...
As I have pointed out, what happen if u inserted a row, next time you get this, you number will not match the previous.
@Churk As long as you don't specify an ORDER BY prt_id clause or delete a value then the ids should always stay the same.
If u don't delete. That is not something u can guarantee. Especially for the life of an application.
1

You could do something like:

<?php

$counter = 1; // Start at one for first entry

$res = mysql_query("SELECT * FROM participants WHERE prt_id = 12");

while( $array = mysql_fetch_assoc($res) )
{
    // Do something with the counter, store it into array with details
    $counter++;
}
?>

Comments

1

This should do what you want inside MySQL (ie assign a rownum in the order of prt_id), but the performance will be dependent on the number of rows in the table so it's not optimal.

SELECT * FROM (
  SELECT @tmp:=@tmp+1 rownum, p.*
  FROM (SELECT @tmp:=0) z, participants p
  ORDER BY prt_id
) participants
WHERE prt_id = 36;

Demo here.

Edit: This "doh level" rewrite uses an simple index range instead of a table scan, so should be much faster (provided prt_id is a PRIMARY KEY)

SELECT *, COUNT(p2.prt_id) ROWNUM
FROM participants p1
JOIN participants p2
  ON p1.prt_id >= p2.prt_id
WHERE p1.prt_id=36;

Demo here.

1 Comment

Great answer but like you said, this would be very slow with even a minimal amount of rows. +1
0

you could just add an index column in your database, set it as int, primary key and auto increment. then when retrieving the row you retrieve the index number.

1 Comment

That is the same idea as using one of the primary key column, just because u assign auto increment, if you delete a row and insert another, you would not be reusing the deleted row's id.
0

RowID is a feature of Oracle: http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm.

MySQL does not have something like that, you can basically emulate that by assign number to an array inside php as you retrieve each row, but that doesn't guarantee you the same number next time you retrieve that results. You probably have to settle for using one of the primary IDs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.