I have a field in database named experience and it has values like 5 Years or 2 Years etc. Now I want to search all users having experience between 2 years to 5 years. How can I do that? How can I apply a range on string like 2 years.
2 Answers
you can try this
DB::table('experience')->where(function ($query) use ($YOURVARIABLE) {
$query->where('2years', '<=', $YOURVARIABLE);
$query->where('5years', '>=', $YOURVARIABLE);
})->get();
2 Comments
Usman Iqbal
but please can you tell me how it happens that a string is dealing like integer on comparison operator?
Siva Ganesh
In the above case, I just checking an where condition using where condition it check the users between 2 to 5 years experience
first of all take all values from data with the years, store them in a array. than remove the the static 'Years' word from the string, you will get number, than apply range query on it. select * from YOURTABLE where YEAR_RANGE 'FIRST_VALUE' and 'SECOND_VALUE' order by YEARS desc;
1 Comment
Usman Iqbal
but dont you think it will make query slow?