Exact comma delimited search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cain Johnson
    New Member
    • May 2011
    • 1

    Exact comma delimited search

    I have searched for many days on google on this. And I have learned quite a bit. I'm stuck on one part of my code I cannot get to work.

    i have a field called `allgenreid` which list all the game genres by numerical id (eg. 1,2,3,4,5 ... 200)

    I used to use the following

    Code:
    SELECT * FROM table_name WHERE `allgenreid` LIKE '%$id%'
    however this returns many undesirable results. If i am looking for gamegenre #5, it would return 5,15,25,35...15 5 etc.. NOT what I want

    Later I learned of a sql command: FIND_IN_SET(). That is perfect. Once problem I realized was that the following does not work

    Code:
    SELECT * FROM table_name WHERE FIND_IN_SET('$id',`allgenreid`)
    I didnt realize it, until i debugged that is was comparing $id to the string "allgereid" and not `allgenreid` 's contents

    How do I go about getting an exact search? There seem o be millions of results on Google (spent a week looking) and most of the answers I tried, do not work.

    I think I have searched so long that now I'm missing the obvious LOL
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Code:
    SELECT * FROM table_name WHERE `allgenreid` = $id
    IF you are only trying to pull records for a specific id the above query should work. If allgenreid is numeric you don't need the quotes around $id.

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Can you provide plain query instead of PHP code?

      Code:
      SELECT * FROM table_name WHERE FIND_IN_SET('$id',`allgenreid`)
      This query will not return desired results if there is a comma in $id. To workaround this you can try this
      Code:
      SELECT * FROM table_name WHERE FIND_IN_SET('5',`allgenreid`) AND FIND_IN_SET('7',`allgenreid`)

      Comment

      Working...