Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using BETWEEN in WHERE condition

Tags:

I'd like the following function to select hotels with an accomodation between a certain $minvalue and $maxvalue. What would be the best way to do that?

function gethotels($state_id,$city,$accommodation,$minvalue,$maxvalue,$limit,$pgoffset)
    {
        $this->db->limit($limit, $pgoffset);
        $this->db->order_by("id", "desc");
        $this->db->where('state_id',$state_id);
        $this->db->where('city',$city);

        // This one should become a between selector
        $this->db->where($accommodation,$minvalue); 

        $result_hotels = $this->db->get('hotels');
        return $result_hotels->result();

   }
like image 922
Viju Vijay Avatar asked Mar 30 '12 10:03

Viju Vijay


People also ask

Can we use between in WHERE clause?

The SQL BETWEEN operator is used along with WHERE clause for providing a range of values. The values can be the numeric value, text value, and date.

How do you use between in?

In between should always appear as two words. Although inbetween is common, it is a misspelling and does not appear in any English dictionary. Unnecessarily adding in to between is also a common grammatical mistake. As a compound adjective, in-between should be hyphenated.

What is the purpose of the between operator in a WHERE clause?

In SQL, the BETWEEN operator is useful to get the values within the defined range. Generally, we will use BETWEEN operator in the WHERE clause to get the required values within the specified range.

What is a Between condition?

A BETWEEN condition determines whether the value of one expression is in an interval defined by two other expressions. between_condition::= Description of the illustration ''between_condition.gif'' All three expressions must be numeric, character, or datetime expressions.


2 Answers

You should use

$this->db->where('$accommodation >=', minvalue);
$this->db->where('$accommodation <=', maxvalue);

I'm not sure of syntax, so I beg your pardon if it's not correct.
Anyway BETWEEN is implemented using >=min && <=max.
This is the meaning of my example.

EDITED:
Looking at this link I think you could write:

$this->db->where("$accommodation BETWEEN $minvalue AND $maxvalue");
like image 114
Marco Avatar answered Sep 24 '22 14:09

Marco


In Codeigniter This is simple Way to check between two date records ...

$start_date='2016-01-01';
$end_date='2016-01-31';

$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
like image 23
Muhammad Fahad Avatar answered Sep 25 '22 14:09

Muhammad Fahad