2

I have values like something

 | UserName   |    SkillsID      |
 |------------|------------------|
 | Sohil      |    1,2,15,16,19, |
 | Ankur      |    5,8,14,19,    |
 | Lalu       |    4,3,14,15,    |
 | Vinod      |    5,            |
 | Milind     |    8,11          |

now I want to search the Users who have got SkillsID = 5 then result would

 | UserName   |    SkillsID      |
 |------------|------------------|
 | Ankur      |    5,8,14,19,    |
 | Vinod      |    5,            |

How my query would be?

I have thought to split SkillsID into table & then perform query on it but i am not getting any idea to split. can anybody help me???

Thanks in advance to helpers..

3 Answers 3

2

You can do it with FIND_IN_SET() function or regex:

SELECT * FROM t WHERE SkillsID REGEXP '(^|,)5($|,)'

But you're violating relation DB principle with storing multiple values in one field. You should create a link table for storing that properly and make a foreign key to your user table in it.

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

1 Comment

thank you for help its working. I know i am violating database rules but joins took more time to get records.
2

Never, never, never store multiple values in one column!

As you see now this will only result in problems. Please normalize your DB structure first like this

 User table  
+-------------------+ 
| userid | username |
+-------------------+
|   1    |  Sohil   |
|   2    |  Ankur   |
+-------------------+

 skill table
 --------------------
 | Userid| SkillID  |
 |-------|----------|
 | 1     |    1     |
 | 1     |    2     |
 | 1     |    15    |
 | 1     |    16    |
 ...
 | 2     |    5     |
 | 2     |    8     |
 | 2     |    14    |
 ...

Comments

0

you can seperate values virtually into different columns using following query and then you can run query using this query as sub query.

select 
substring_index(column_name,',',1) c1,
substr(column_name,length(substring_index(column_name,',',1))+2,length(substring_index(column_name,',',2))-length(substring_index(column_name,',',1))-1) c2,
substr(column_name,length(substring_index(column_name,',',2))+2,length(substring_index(column_name,',',3))-length(substring_index(column_name,',',2))-1) c3,
substr(column_name,length(substring_index(column_name,',',3))+2,length(substring_index(column_name,',',4))-length(substring_index(column_name,',',3))-1) c4,
substr(column_name,length(substring_index(column_name,',',4))+2,length(substring_index(column_name,',',5))-length(substring_index(column_name,',',4))-1) c5,
substr(column_name,length(substring_index(column_name,',',5))+2,length(substring_index(column_name,',',6))-length(substring_index(column_name,',',5))-1) c6
from table_name

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.