Jump to content

Archived

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

Ninjakreborn

searching with and, and or

Recommended Posts

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??

Share this post


Link to post
Share on other sites
$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..


Regards
Liam

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
$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 search

Any good?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
If mixing AND and OR, alway use parentheses to remove ambiguity

So 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"

Share this post


Link to post
Share on other sites

×

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.