Jump to content


Photo

searching with and, and or


  • Please log in to reply
5 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 22 September 2006 - 04:12 PM

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

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 22 September 2006 - 04:30 PM

$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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 22 September 2006 - 04:35 PM

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

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 22 September 2006 - 04:42 PM

$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?
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 22 September 2006 - 04:49 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 22 September 2006 - 06:15 PM

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"
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users