MySQL query with AND, OR statements [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amh1010
Forum Newbie
Posts: 19
Joined: Sat Feb 06, 2010 1:49 pm

MySQL query with AND, OR statements [SOLVED]

Post 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?
Last edited by amh1010 on Mon Mar 01, 2010 8:54 am, edited 1 time in total.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: MySQL query with AND, OR statements

Post 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.
amh1010
Forum Newbie
Posts: 19
Joined: Sat Feb 06, 2010 1:49 pm

Re: MySQL query with AND, OR statements

Post by amh1010 »

Works perfectly. Thanks
Post Reply