Dustin Vicent
Dustin Vicent

Reputation: 19

Can I use two Where in select statement

I'm trying to get a date range and a transaction type in a query. I'm working in PHP with a Mysql database. Originally, the statement was

         ("select * from accounting WHERE date between '$startdate' AND 
        '$enddate'",$prop);

It works beautifully, no issues @ all. But I also need it to select a column in the database called transaction type. Here's how I modified it:

           ("SELECT date, account, description, Income FROM accounting where 
           transactiontype = 'Income' and WHERE date between '$startdate' AND 
           '$enddate' order by date ASC",$prop);

I'm getting the error message: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE date between '2012/09/01' AND '2013/04/17' order by date ASC' at line 1 Query: SELECT date, account, description, Income FROM accounting where transactiontype = 'Income' and WHERE date between '2012/09/01' AND '2013/04/17' order by date ASC Any Help would be greatly appreciated.

Upvotes: 0

Views: 149

Answers (3)

user1945782
user1945782

Reputation:

("SELECT date, account, description, Income FROM accounting where 
  transactiontype = 'Income' and "date" between '$startdate' AND 
 '$enddate' order by date ASC",$prop);

You may also need to enclose the date in speech marks or ticks (`), as indicated.

Upvotes: 0

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

AND is enough, you don't need the seconde WHERE.

("SELECT `date`, account, description, Income 
  FROM accounting 
  WHERE transactiontype = 'Income' 
  AND `date` between '$startdate' 
  AND '$enddate' order by `date` ASC",$prop);

By the way, try to avoid reserved keywords as table / columns name (like date). You'll have to escape them all the time (and will forget that all the time also).

Upvotes: 2

Shafqat Masood
Shafqat Masood

Reputation: 2570

kindly remove the second where clause..

       (SELECT date, account, description, Income FROM accounting where 
       transactiontype = 'Income' and (date between '$startdate' AND 
       '$enddate') order by date ASC",$prop )

Upvotes: 0

Related Questions