2
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. :)

4
  • Please have a look at MySQL BETWEEN ... AND ... operator. Commented Oct 14, 2015 at 5:54
  • 3
    First update the schema to use proper Date type for your date field, then run a simple between comparison using the right formats Commented Oct 14, 2015 at 5:57
  • convert your date to mysql date format. Commented Oct 14, 2015 at 5:59
  • Right, search for BETWEEN. You can also use <, >, <=, >= compare on dates in WHERE Commented Oct 14, 2015 at 5:59

2 Answers 2

2

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');
Sign up to request clarification or add additional context in comments.

Comments

-1

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'];
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.