Jump to content

Find in set


karenn1

Recommended Posts

Hey everyone,

 

I have a page where a user can select one or more project types from a multiple select box. When they click on Search, I would like to query a field in the database and return the values that match the search. This is my code:

 

SELECT * FROM projects WHERE FIND_IN_SET('Agriculture,Environment', project_type)

 

If I put in Agriculture on its own, it returns rows. Likewise with Environment so I know there's data for both. but it's not working together. Here's my data structure:

 

id             name                project_type
1              Project 1           Agriculture
2              Project 2           Agriculture;Environment
3              Project 3           Environment

 

This is how I inherited this database. The project type data is delimited with a semi-colon.

 

Can someone please help?

 

 

Thanks in advance!

 

Karen

Link to comment
Share on other sites

Fenway's comment leads directly to a repeat of Funster's question:  Why aren't you redesigning this?  How much code could possibly be touching this table?  Breaking this list out into its own table properly will give you a good database design that can be used for real tasks.

 

-Dan

Link to comment
Share on other sites

Hi guys,

 

Thanks for the reply. To answer your question Dan, I quoted a certain amount of time to do a few specific things on this site. Unfortunately, I didn't quote extra to redesign the database and it will take me some time to do. I won't get paid for that time. I understand what you are saying though but in this case, I just can't do it that way. I need to make it work in it's current format.

 

I'll try the REPLACE() and use commas instead and see if that works.

 

 

Karen

Link to comment
Share on other sites

I actually found something that works well:

 

SELECT * FROM projects WHERE MATCH (project_type) AGAINST ('Agriculture, Environment')

 

This finds all the rows where the project type is Agriculture and/or Environment.

 

Are there any draw backs in using this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.