Scenario: I am making a medical report where I need the latest test result plus two past results (if the customer has taken the test in the past).
Consider this db:
id | patient_id | test_id | value
1 | 3 | 5 | 10
2 | 6 | 8 | 14
3 | 3 | 5 | 12
4 | 9 | 3 | 18
5 | 3 | 10 | 4
6 | 4 | 15 | 10
7 | 3 | 5 | 19
8 | 3 | 5 | 29
So if the patient comes and asks for the report [row = 7], I need to show results of the past two records [row = 3 and row = 1] having the same patient_id and test_id.
What I have tried:
$records = mysqli_query($con, "select * from value_based_result where patient_id='$ptid' and test_id='$testid' order by (id='$id') DESC, id limit 3 ") or die(mysqli_error());
while($record=mysqli_fetch_array($records)){
echo $record['value'];
}
Now the issue is, this query is not starting ordering by from current (id=7). It is just bringing (id=7) result to the top in query. What is the best way to achieve this?