By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Exact comma delimited search

P: 1
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

Expand|Select|Wrap|Line Numbers
  1. 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...155 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

Expand|Select|Wrap|Line Numbers
  1. 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
May 15 '11 #1
Share this Question
Share on Google+
2 Replies


JKing
Expert 100+
P: 1,206
Expand|Select|Wrap|Line Numbers
  1. 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.
May 16 '11 #2

mwasif
Expert 100+
P: 801
Can you provide plain query instead of PHP code?

Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table_name WHERE FIND_IN_SET('5',`allgenreid`) AND FIND_IN_SET('7',`allgenreid`)
Jun 5 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.