0

My MySQL field having skills field(string) like,

1)php,mysql
2)java
3)java,c

I need to select the column with skills field condition. Condition may be Like,

skills="php"    // o/p => 1st column
or
skills = "mysql,c"   // o/p => 1st and 3rd column

How I can write Query.? Here im using PHP.

2
  • KarSho, any change you apply the First normal form and get your skill field in other table? Commented May 6, 2013 at 5:49
  • @medina ya, thanks for your !dea. But, i designed DB fully. Now i can't change. Commented May 6, 2013 at 5:51

2 Answers 2

3

One way to do it

SELECT *
  FROM table1
 WHERE skills LIKE '%mysql%' 
    OR skills LIKE '%c%'

SQLFiddle

It would be much easier if you'd normalized your data

Sign up to request clarification or add additional context in comments.

5 Comments

My search condition may having 'n' content
@KarSho What do you mean by '...having 'n' content'?
@KarSHo What should happen when search condition is empty? Show all records or doesn't show anything?
@KarSho Well then in your php code if search condition is empty don't add WHERE clause, if it's not empty split it and build up your WHERE clause with as many OR's as you have keywords
potentially unlimited number of OR clauses?
1

Use a procedure like this one:

Split comma separated values from one column to 2 rows in the results. MySQL

To split your csv skills field into a temp table with rows for each skill

Then do the same thing with your search parameters (which I assume is also csv string passed in)

Then simply apply a JOIN over the two results and get the DISTICT Id.

there are many solutions for splitting a comma separated string in MySQL (though it's not particularly good at this task)... if you can't properly normalize the data then you can't very well complain about inefficient query routines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.