i have a column in database that stores a range of user ages, like 5-10, 10-18, 18-25….
Now i want to write a query to fetch the columns with age matching this range.
For example, if i pass the age as 12, it should fetch the columns that fit in the range 10-18.
If i pass the age as 10, it should fetch the columns that fit in the range 5-10.
But the fact is that, i am storing the range as a varchar type (there is a hyphen in between).
So i am curious about how to achieve this, and how to construct a query.
You could split up the values using
SUBSTRING_INDEXThis query asumes agespan is the column containing
5-10,12-18.SUBSTRING_INDEX(
agespan,’-‘, 1) matches everything in the string before the first occurence of-SUBSTRING_INDEX(
agespan,’-‘, -1) matches everything in the string after the last occurence of-