Guys belwow query is returning nothing.please help to write correct query
$month = 4;
$Pmonth = 1;
$year = 10;
$Pyear =11;


$sql=mysql_query("SELECT * FROM `expense` WHERE month BETWEEN '$month' AND '$Pmonth' AND year BETWEEN '$year' AND '$Pyear'");

Recommended Answers

All 8 Replies

What format are the month and year in the database?

i had made seprate col for month day and year.i want to wrtie a query like if i want top get data of month one year 2010 to month one of 2011 or any date.

$select = mysql_query ("SELECT * FROM expense WHERE year = '$year' AND month = '$month' OR year = '$Pyear' AND month = '$Pmonth'");

this is my query yet but it only retrieve record select not records between then

You need to put your AND comparisons in brackets as you also have an OR in them

$select = mysql_query ("SELECT * FROM expense WHERE (year = '$year' AND month = '$month') OR (year = '$Pyear' AND month = '$Pmonth')");

Though this isn't the original query you posted.

SELECT * FROM expense WHERE year = '10' OR month = '2' AND year = '11' OR month = '2'

i used this query but it showed me record from month one but as query it should show me record from second of 2010 but it should show from first month

You haven't put the different parts of the query in brackets as I showed you and your query makes no sense at all (why do you have OR MONTH = 2 in there twice).

Why not run the query I posted?

Stepping back a bit, why don't you have a single column for your date information and make use of MySql date and time functions to deal with your query? MySql will handle such things both more quickly and more robustly than your program. See http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html Replace the /5.6/ with whatever version is yours if you need to (recent changes are about the precision of partial seconds, which I doubt matters to you). See particularly the MONTH() and YEAR() functions which will extract the month and year from a date (or datetime or timestamp)

I totally agree with griswolf:)

I would be wary of using BETWEEN where the first value is greater than the second value. I'm not sure what would be returned in that instance. The two BETWEEN clauses need to be surrounded by brackets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.