Kasuke_Akira Posted March 17, 2007 Share Posted March 17, 2007 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 More sharing options...
chiprivers Posted March 17, 2007 Share Posted March 17, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43094-solved-mysql-query-conditions/#findComment-209342 Share on other sites More sharing options...
Kasuke_Akira Posted March 17, 2007 Author Share Posted March 17, 2007 Thanks Wasn't sure if I was gonna get a response. Link to comment https://forums.phpfreaks.com/topic/43094-solved-mysql-query-conditions/#findComment-209346 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.