I have a column in my table which contains the values 0 or 1.
Now I want to select the start of the last period of 1’s.
Imagine I’ve the next table with 2 columns. The last period of 1’s are the ID’s 4 and 3.
The start of this period is ID = 3.
ID – VALUE
- 5 – 0
- 4 – 1 <– last period
- 3 – 1 <– last period
- 2 – 0
- 1 – 1 <– period longer ago
- 0 – 0
But how do I select this record with one query (and maybe a bit of PHP) and without select all records?
Edit:
Using MySQL
You can try finding highest id with value 1 for which closest lower id has value 0.
In sql this would be: