0
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

Qirel
  • 25,449
  • 7
  • 45
  • 62

5 Answers5

0

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
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

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();
aynber
  • 22,380
  • 8
  • 50
  • 63
0

In Laravel you can use whereBetween and orWhereBetween() methods:

Job::whereBetween('created_at', [1, 4])->orWhereBetween('created_at', [7, 10])->get();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0
SELECT * 
FROM jobs 
WHERE created_at BETWEEN 1 and 4 or created_at  BETWEEN 7 and 10;
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
0

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

MohamedSabil83
  • 1,529
  • 7
  • 12