My relevant table structure is like so (with id
being primary key and having autoincrement):
id | pid | type | distance | pspeed
Each pid
(player id) has a unique row for every type
and pspeed
combination. For example, there is no row with a specific pid
value, a specific type
value, and more than one pspeed
value. Likewise for pid
and pspeed
, there is not more than one row with the same type
value.
My goal is to, for every type
(there are 26 total), group by pspeed (there are 7 different pspeed values) and sort by distance descending until a specific pid
value is found and return that row with the "rank" or "position" of that row. Basically I want to do a ORDER BY distance
for each type
and pspeed
until a specific pid
value is reached.
Is this possible via pure queries? I know how to do this with several queries and loops in a PHP script but I would like to be able to minimize the code I have to write. Unfortunately, I'm quite positive it would require SQL knowledge that is beyond me.
If counting up until a specific pid
value is not possible I can just bake that into my PHP script to determine the "rank".
Edit: here is how I'm currently doing this (using CodeIgniter):
$res = array();
$q1 = $this->db1->get_where('uq_players', array('authid' => $authid), 1);
if($q1->num_rows()){
foreach($this->jtype as $type => $value){
foreach($this->pspeed as $speed){
$i = 0;
$this->db1->order_by('distance', 'desc');
$q2 = $this->db1->get_where('uq_jumps', array('type' => $type, 'pspeed' => $speed));
foreach($q2->result() as $row){
$i += 1;
if($row->pid === $q1->row()->id){
$res[] = self::create_array($row, $type, $row->wpn, $i);
break;
}
}
}
}
}
And some sample data.