Page 1 of 1

MySQL query with AND, OR statements [SOLVED]

Posted: Mon Mar 01, 2010 1:03 am
by amh1010
I want a MySQL query that will display all the real estate properties with 3 bedrooms, 2 baths, and are either houses or duplexes. Here's my query:

$row=mysql_query('SELECT * FROM props WHERE bed="3" AND bath="2" AND type="house" OR type="duplex"');

But it obviously doesn't work. How can I get this working?

Re: MySQL query with AND, OR statements

Posted: Mon Mar 01, 2010 2:03 am
by davex

Code: Select all

$row=mysql_query("SELECT * FROM props WHERE bed='3' AND bath='2' AND (type='house' OR type='duplex')");
Should do it. Your original query would, owing to how MySQL clauses work, return;

All rows where bed=3, bath=2 and type=house
OR all rows where type=duplex

Putting the type into brackets will return only rows where bed=3 and bath=2 and the type is either house or duplex.

HTH.

Cheers,

Dave.

Re: MySQL query with AND, OR statements

Posted: Mon Mar 01, 2010 8:54 am
by amh1010
Works perfectly. Thanks