选择具有特定号码的所有号码

时间:2017-10-26 22:34:46

标签: mysql

我有一个关于mysql大师的问题。 我有一张这样的桌子:

     | col1  | col2         |
row1 | John  | 1,2,13,22,52 |
row2 | Dave  | 1,8,10       |
row3 | Maria | 2,3,5,11     |

如何编写检查“col2”中特定数字的查询。例如,我希望所有行中编号为“1”的行不是“13”,或“11”而是“1”。所以它将返回row1和row2。

我试图用“LIKE%”来做这件事,但那不起作用

SELECT * FROM table WHERE col2 LIKE "%1%"

有什么建议吗?

修改

分成两个表?

table1
     | id | col1  | 
row1 | 1  | John  | 
row2 | 2  | Dave  | 
row3 | 3  | Maria | 
table2
     | id | f_id | col2 |
row1 | 1  | 1    | 1    |
row1 | 2  | 1    | 2    |
row1 | 3  | 1    | 13   |
row1 | 4  | 1    | 22   |
row1 | 5  | 1    | 52   |
row2 | 6  | 2    | 1    |
row2 | 7  | 2    | 8    |
row2 | 8  | 2    | 10   |
row3 | 9  | 3    | 2    |
row3 | 10 | 3    | 3    |
row3 | 11 | 3    | 5    |
row3 | 12 | 3    | 11   |

1 个答案:

答案 0 :(得分:0)

您必须使用函数FIND_IN_SET

SELECT * FROM table WHERE FIND_IN_SET('1', col2) > 0;

如果你要使用分隔表:

SELECT *
FROM table1
JOIN table2 ON table1.id = table2.f_id
WHERE table2.col2 = 1;