Search code examples
mysqlmysql5

Selecting all fields with values greater than a current field value


I have a table that looks like this.

| path_id | step | point_id | delay_time | stand_time | access |

| 202 | 1 | 111 | 0 | 0 | 7 |

Which lists point_id's in step order. E.g.: 111 - step 1, 181 - step 2, etc.

I need to write a query that would take point_id, select ALL values which have higher step within ALL path_id's that have a given value and return a grouped set of point_id's.

I am currently using this query

SELECT DISTINCT `pdb`.`point_id` AS `id` 
FROM `path_detail` AS `pda` INNER JOIN 
`path_detail` AS `pdb` ON pda.path_id = pdb.path_id 
 AND pda.step < pdb.step
WHERE 
(pda.point_id = 111) 
GROUP BY `pdb`.`path_id`

Which doesn't seem to work too reliably.

Any suggestions?


Solution

  • Try:

    SELECT Distinct `pdb`.`point_id` AS `id` 
    FROM `path_detail` AS `pda`, `path_detail` AS `pdb`  
    WHERE 
    pda.point_id = 111
    AND pda.path_id = pdb.path_id 
    AND pda.step < pdb.step
    Order by `pdb`.`point_id` ASC