Kevster
Kevster

Reputation: 35

mySQL multiple AND comparisons in WHERE statement

I have the following mySQL statement which is incorrect.

SELECT id, location__, image__, bedrooms, bathrooms, price__ 
FROM myTable 
WHERE LOWER(location__) REGEXP '.london.' 
AND (bedrooms >=2) 
AND (bathrooms == true) 
ORDER BY price__  DESC 
LIMIT 0,20;

How do I format a SQL query that works in PHP so that I can compare bedrooms > 2 AND bathrooms > 2 AND ensuites = 1 (i.e. true)... etc

Upvotes: 0

Views: 1423

Answers (2)

Rams
Rams

Reputation: 2159

You just need to add condition as you like with AND to fulfill your requirement here.

SELECT id, location__, image__, bedrooms, bathrooms, price__ 
FROM myTable 
WHERE LOWER(location__) REGEXP '.london.' 
AND bedrooms >=2
AND bathrooms >=2 and ensuites = 1
ORDER BY price__  DESC 
LIMIT 0,20;

Upvotes: 2

Blackbam
Blackbam

Reputation: 19396

You may be using a wrong SQL comparison operator with bathrooms. In SQL you compare with a single equal-sign:

SELECT id, location__, image__, bedrooms, bathrooms, price__ 
FROM myTable 
WHERE LOWER(location__) REGEXP '.london.' 
AND bedrooms >= 2 
AND bathrooms = 1 
ORDER BY price__  DESC 
LIMIT 0,20;

https://www.techonthenet.com/sql/comparison_operators.php

Upvotes: 1

Related Questions