Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a BETWEEN condition with Codeigniter's query builder methods?

How to write select query with between similar to this with active record?

SELECT *
FROM test_tbl
WHERE date BETWEEN '$start' and '$end'
ORDER BY ID
like image 559
Gihan Lasita Avatar asked Dec 13 '25 06:12

Gihan Lasita


1 Answers

AFAIK, there's no built-in support for BETWEEN

You can do this instead

$this->db->where("date BETWEEN '$start' AND '$end'");
$this->db->get("test_tbl");  

Or write a helper function that look like this

function where_between($field, $min, $max){
     $CI = get_instance();
     return $CI->db->where("`$field` BETWEEN '$min' AND '$max'");
}  

Later on, you can use that function by calling it like where_between('test_tbl', $start, $end)

like image 86
Kemal Fadillah Avatar answered Dec 14 '25 19:12

Kemal Fadillah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!