如何将逗号分隔值与mySql中的其他列匹配

时间:2013-10-01 09:22:47

标签: mysql

我有以下两个表:

用户:

+---------+--------------+
| user_id | skills       |
+---------+--------------+
|       1 | 1,2,3,5,4,14 |
|       2 | 1,2,3        |
|       3 | 3,4,5        |
|       4 | 1,2          |
+---------+--------------+

pskills:

+-----+--------+------+----------+
| PID | SKILLS | SPLI | status   |
+-----+--------+------+----------+
|   1 | 2,4    |    1 |          |
|   1 | 1      |    1 | required |
+-----+--------+------+----------+

我想匹配表 pskills SKILLS 列的值。例如,如果使用第一行 pskills 进行查询并使用用户表加入,则它将返回用户ID 1,因为SKILLS 2,4仅与用户ID 1匹配。我怎么能这么容易做到?

1 个答案:

答案 0 :(得分:4)

永远不要在一列中存储多个值!

你应该像这样规范你的表

**user**
+---------+--------------+
| user_id | skills       |
+---------+--------------+
|       1 | 1            |
|       1 | 2            |
|       1 | 3            |
|       1 | ...          |
|       2 | 1            |
|       2 | 2            |
|         | ...          |
+---------+--------------+

**pskills**
+-----+--------+------+----------+
| PID | SKILLS | SPLI | status   |
+-----+--------+------+----------+
|   1 | 2      |    1 |          |
|   1 | 4      |    1 |          |
|   1 | 1      |    1 | required |
+-----+--------+------+----------+