Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:39:34+00:00 2026-06-12T13:39:34+00:00

I have a data stored as comma separated in the database field. Like say

  • 0

I have a data stored as comma separated in the database field. Like say in a field colors there are following rows.

red,blue, green
blue, white, purple, pink
yellow, khaki, maroon, orange
gray, lemon, black, brown

Next user has a textbox in a html form, where he enters a colors in a comma separated form. I want to search the database to return records matching his input. For example.

If I enter red, purple, orange, then the query should return three rows because first row has red, second row has purple and third has orange.

I m not getting the way to easier than breaking the user inputs into array and looping through each colors and search the database to get results. Is there any easier way to do this?

Edit

I know storing data in comma separated form is not a good practice but I m working on a updates on application which was made by other programmer. And at this stage I cannot afford to change table structures.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:39:35+00:00Added an answer on June 12, 2026 at 1:39 pm
    1. Of course normalisation is the best approach if it is an option:

      CREATE TABLE colors ( color VARCHAR(6) NOT NULL PRIMARY KEY );
      
      INSERT INTO colors VALUES
        ('red'), ('blue'), ('green'), ('white'), ('purple'), ('pink'),
        ('yellow'), ('khaki'), ('maroon'), ('orange'), ('gray'),
        ('lemon'), ('black'), ('brown');
      
      CREATE TABLE itemColors (
        item_id BIGINT UNSIGNED NOT NULL,
        color   VARCHAR(6)      NOT NULL,
        FOREIGN KEY (item_id) REFERENCES my_table (id),
        FOREIGN KEY (color)   REFERENCES colors   (color)
      );
      
      INSERT INTO itemColors (item_id, color)
        SELECT my_table.id, colors.color
        FROM   my_table JOIN colors ON FIND_IN_SET(colors.color, my_table.colors);
      
      ALTER TABLE my_table DROP COLUMN colors;
      

      Then one would merely need do:

      SELECT *
      FROM   my_table
      WHERE  id IN (
                     SELECT item_id
                     FROM   itemColors
                     WHERE  color IN ('red','purple','orange')
                   )
      
    2. Otherwise you could build a query (in your application) that uses MySQL’s FIND_IN_SET() function:

      SELECT *
      FROM   my_table
      WHERE  FIND_IN_SET('red',    colors)
          OR FIND_IN_SET('purple', colors)
          OR FIND_IN_SET('orange', colors)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario: I have various user's data stored in my database.
I have a data stored in a xml column and need a comma-separated list
I have data stored as below in an MS Access database: Date User 20090101
I have objects with location data stored in Core Data, I would like to
I have a database which contains picture data stored as a binary blob. The
Environment: SQL Server 2005 I have a stored proc which receives comma separated value
If I have a column called 'Categories' with say science,maths,english in the row comma-separated
I have a field in a data feed coming in with some values separated
I have stored some data in MySQL database through a java program. Now I
I have data stored in comma delimited txt files. One of the columns represents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.