Vaidas
Vaidas

Reputation: 1028

Getting results within two dates range

MySQL table have 3 rows.

Table:

| ID | StartDate  | EndDate      |
|:--:|:----------:|:------------:|
| 1  | 2014-07-01 | 2014-07-31   |
| 2  | 2014-07-15 | 2014-08-15   |
| 3  | 2014-08-01 | 2014-08-31   |

Visual example:

| 07-01 07-31 | 08-01 08-31 |
|:-----------:|:-----------:|
| <result 1>  |             |
|         <result 2>        |
|             | <result 3>  |

I'm providing two dates, Start Date and End Date.

Question:

After setting Start Date to 2014-07-01 and End Date to 2014-07-31 I need to get result 1 and result 2.

What is best way of doing this?


Edit:

End up doing this:

SELECT id, StartDate, EndDate FROM table

WHERE (
    (StartDate BETWEEN DATE('2014-07-01') AND DATE('2014-07-31'))
    OR
    (EndDate   BETWEEN DATE('2014-07-01') AND DATE('2014-07-31'))
);

Thanks to Yograj Sudewad and Sadikhasan. You guys are awesome!

Upvotes: 0

Views: 53

Answers (4)

Yograj Sudewad
Yograj Sudewad

Reputation: 343

Try this query:

SELECT * FROM table 
WHERE StartDate BETWEEN DATE('2014-07-01') AND DATE('2014-07-31');

Upvotes: 2

Sadikhasan
Sadikhasan

Reputation: 18600

Try this

select * from table_name where startdate between "2014-07-01" AND "2014-07-31"

Upvotes: 1

Torrezzzz
Torrezzzz

Reputation: 307

select * from table_name where startdate ='$datefrom' and enddate='$dateto'

Try this.

Upvotes: 0

Tamas Kinsztler
Tamas Kinsztler

Reputation: 181

SELECT * FROM table WHERE StartDate >= '$dateFrom' AND StartDate <= '$dateTo';

Upvotes: 1

Related Questions