SELECT * from jobs where created_at BETWEEN IN('1' and '4', '7' and '10');
this is my sql which i tried but it gives error.thanks in advance for your help
SELECT * from jobs where created_at BETWEEN IN('1' and '4', '7' and '10');
this is my sql which i tried but it gives error.thanks in advance for your help
You can use two BETWEEN ... AND ...
queries with an OR
:
SELECT *
FROM jobs
WHERE created_at BETWEEN 1 AND 4
OR created_at BETWEEN 7 AND 10
You need to separate out your BETWEEN statements:
SELECT * from jobs where created_at BETWEEN 1 and 4 OR created_at BETWEEN 7 AND 10;
For Laravel's Query Builder:
DB::table('jobs')->whereBetween('created_at', [1,4])
->orWhereBetween('created_at', [7,10])
->get();
In Laravel you can use whereBetween
and orWhereBetween()
methods:
Job::whereBetween('created_at', [1, 4])->orWhereBetween('created_at', [7, 10])->get();
SELECT *
FROM jobs
WHERE created_at BETWEEN 1 and 4 or created_at BETWEEN 7 and 10;
if you mean by [1, 3, 7, 10] the months, use this:
I guess your model is Job
Job::where(function($q){
$q->whereMonth('created_at', '>=', 1)
->whereMonth('created_at', '<=', 3);
})->orWhere(function($q){
$q->whereMonth('created_at', '>=', 7)
->whereMonth('created_at', '<=', 10);
})->get();
Note1: if mean by numbers the days not months, use whereDay
instead of whereMonth
Note2: created_at is a timestamp as default