susanloek
susanloek

Reputation: 411

Use OR and AND in MySQL Query

I am trying to use the OR and AND selector together in a MySQL query, but the last AND has no effect on the result. This is my current SQL query:

$sql->setQuery("SELECT * FROM rex_projekte 
                                WHERE projektname LIKE '%$suche%' 
                                    OR projektnummer_hw LIKE '%$suche%' 
                                    OR projektnummer_k LIKE '%$suche%' 
                                AND gruppe = 'new'");

Upvotes: 2

Views: 63

Answers (2)

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

Try this code

$sql->setQuery("SELECT * FROM rex_projekte 
                                WHERE (projektname LIKE '%$suche%' 
                                    OR projektnummer_hw LIKE '%$suche%' 
                                    OR projektnummer_k LIKE '%$suche%') 
                                AND gruppe = 'new'");

Upvotes: 2

ThinkTank
ThinkTank

Reputation: 1191

Juste use brackets

SELECT * 

FROM rex_projekte 

WHERE 

( projektname LIKE '%$suche%' OR projektnummer_hw LIKE '%$suche%' OR projektnummer_k LIKE '%$suche%' )

AND gruppe = 'new'

Upvotes: 2

Related Questions