Reputation: 21
date1 = 10/1/2015
date2 = 10/13/2015`
How to fetch data from database using these two dates? I want to fetch all the data that was entered from 1 to 13 october. How can I do it using php or SQL? Please guide me to write the SQL queries.
Database design
id name date
1 Ram 10/1/2015
2 shyam 10/2/2015
3 hari 10/13/2015
4 gita 10/14/2015</pre>
I want to select and show first 3 id which are listed between 1st oct
to 13th oct
and don't want to show 14th oct value. Please help me. Looking forward for the help. Thank you in advance. :)
Upvotes: 2
Views: 1128
Reputation: 3143
Use mysql_query function to send query, LIMIT 3 for showing 3 rows and BETWEEN Condition to check the range. Also rather BETWEEN Condition you can use greater than (>=) & less than(<=) bindrelational operators. But your database date column shouldbe in date format.
$result = mysql_query("select id from test where date between '2015-10-01' and '2015-10-13' LIMIT 3 ");
while($row = mysql_fetch_array($result))
{
echo $row['id'];
}
Upvotes: -1
Reputation: 12613
Date format in MySQL is yyyy-MM-dd
. So, the query should be -
select * from test where date between '2015-10-01' and '2015-10-13';
If you want to use a different format to fetch your data then you can use STR_TO_DATE
. Your query will look like this.
select * from test where date between STR_TO_DATE('26-10-2012', '%d-%m-%Y') and STR_TO_DATE('28-10-2012', '%d-%m-%Y');
Upvotes: 2