Jump to content

[SOLVED] mySQL Query Conditions


Kasuke_Akira

Recommended Posts

I have this query being used as a search method for my database:

 

SELECT * FROM cards WHERE $search_field LIKE '%$search_data%' AND cost LIKE '%r%' AND cost LIKE '%u%' ORDER BY $sort_by

 

In the scenario I'll present:

$search_field = name

$search_data = invoke

The lik

So, when all the variable in the search query execute it returns this:

 

name                      cost

Invoke the Firemind - XUUR

 

This result is correct.

 

However if I change the AND between the two 'cost' field variables to an OR like this:

 

SELECT * FROM cards WHERE $search_field LIKE '%$search_data%' AND cost LIKE '%r%' OR cost LIKE '%u%' ORDER BY $sort_by

 

I noticed what it is doing though is grouping the AND ($search_field LIKE '%$search_data%' AND cost LIKE '%r%') as one condition and then what is after the OR (cost LIKE '%u%') is the other condition.

 

However, I want it to be grouped this way:

Condition 1: $search_field LIKE '%$search_data%'

 

AND

 

Condition 2: cost LIKE '%r%' OR cost LIKE '%u%'

 

So I was curious if anyone had a clue how the query string would have to look to accomplish the query the way I need it.

Link to comment
https://forums.phpfreaks.com/topic/43094-solved-mysql-query-conditions/
Share on other sites

Try this:

 


SELECT * FROM cards WHERE $search_field LIKE '%$search_data%' AND (cost LIKE '%r%' OR cost LIKE '%u%') ORDER BY $sort_by


 

Just add in the brackets around the two optional conditions, this will treat them as a combined condition where only one needs to be true.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.