简体   繁体   中英

select from table using comma separated string

I have a user table in which we store skills of a user that is a comma separated ids like ,These are primary key of skill table

'2,3,4,5'

and i have a search page,In which we have to search using the skills that also multiple entry, the data is like this

'2,3' or '3,4' or '6,7' or '2'

my question is how do i write the where clause any one please help thanks in advance

This is too long for a comment.

Your database structure should be fixed. You need a junction table, called something like UserSkills , with one row per user and per id.

Here are some reasons:

  • Storing numbers as strings is a bad idea. You should use the appropriate underlying types.
  • Your skills are presumably references into another table. Foreign key references need to be of the same type as the thing they are referring to.
  • SQL has excellent support for tables and mediocre support for strings, so you should use the appropriate data structure.

All though this is not the right way to store forign keys in the dabase. However, per your problem, you might want to do something like this.

SELECT * FROM tbl_user WHERE skill LIKE '%2, 3%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM