I have a table:
ID - TIME - STATUS
Several STATUS row have '0' value. I want to select all rows that have an ID - 2
from rows with '0' value in the STATUS column, how can I do that?
example:
ID - TIME - STATUS
1 - 00 - null
2 - 03 - null
3 - 02 - 0
4 - 05 - null
From this example I should select row with ID '1', cuz I have '0' value in the row with ID '3'
You can use self join on ids with difference of 2
select * from t
join t t1 on(t.id = t1.id - 2)
where t1.`STATUS` = '0'
I'd do this with a foreach loop, then modify the key as needed:
$data = array(); // Pre loaded data from table.
$ids = array(); // Array of IDs - 2 with status 0.
foreach($data as $key => $row) {
if($row['STATUS'] == 0 && isset($data[$key - 2])) {
$ids[] = $data[$key - 2]['ID'];
}
}