Ninjakreborn Posted September 22, 2006 Share Posted September 22, 2006 I am having to make a query with and's but also with or's.See the ones that have to match are category, subcategory, and schoolname. THen I also want to have like an open source query$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND subcategoryname = '$subcategory' AND schoolname = '$school' OR itemtitle LIKE '%$search%' OR description LIKE '%$search%';";See how i need to make sure it stays within that category, subcategory and schoolname, but the other part is selecting based on different criteria. Is this the right way to go about this?? Quote Link to comment https://forums.phpfreaks.com/topic/21682-searching-with-and-and-or/ Share on other sites More sharing options...
shocker-z Posted September 22, 2006 Share Posted September 22, 2006 $select = "SELECT * FROM $postset WHERE categoryname = '$category' AND subcategoryname = '$subcategory' AND (schoolname = '$school' OR itemtitle LIKE '%$search%' OR description LIKE '%$search%');";i think that will do the trick because u need all the first thing and any of the following OR fields..RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/21682-searching-with-and-and-or/#findComment-96824 Share on other sites More sharing options...
Ninjakreborn Posted September 22, 2006 Author Share Posted September 22, 2006 Well sort of, that is on the right track. I need to search ALL the or fields, and pull results out of whichevers match, but the other's before that, including the school name, HAVE to match 100%. Then everything else I need it to go through checking each one, and pulling out all the results from them Quote Link to comment https://forums.phpfreaks.com/topic/21682-searching-with-and-and-or/#findComment-96827 Share on other sites More sharing options...
shocker-z Posted September 22, 2006 Share Posted September 22, 2006 $select = "SELECT * FROM $postset WHERE categoryname = '$category' AND subcategoryname = '$subcategory' AND schoolname = '$school' AND (itemtitle LIKE '%$search%' OR description LIKE '%$search%')";so we check for all the ands and then for either itemtitle like search or description like searchAny good? Quote Link to comment https://forums.phpfreaks.com/topic/21682-searching-with-and-and-or/#findComment-96830 Share on other sites More sharing options...
Ninjakreborn Posted September 22, 2006 Author Share Posted September 22, 2006 ah perfect, that made it work, and I also just saw that parenthesis thing in w3schools, that was something I didn't know but I am glad to know. Quote Link to comment https://forums.phpfreaks.com/topic/21682-searching-with-and-and-or/#findComment-96832 Share on other sites More sharing options...
Barand Posted September 22, 2006 Share Posted September 22, 2006 If mixing AND and OR, alway use parentheses to remove ambiguitySo if you have "A AND B OR C" you need to show whether that is"A AND (B OR C)" or if it is "(A AND B) OR C" Quote Link to comment https://forums.phpfreaks.com/topic/21682-searching-with-and-and-or/#findComment-96875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.