Select all that have specific number
I have a question for mysql gurus. I have a table like this:
| col1 | col2 |
row1 | John | 1,2,13,22,52 |
row2 | Dave | 1,8,10 |
row3 | Maria | 2,3,5,11 |
How can I write a query that checks for specific number inside "col2". For example I want to have all rows that have number "1" in it not "13", or "11" but "1". So it would return row1 and row2.
I was trying to do that with "LIKE %" but that did not work
SELECT * FROM table WHERE col2 LIKE "%1%"
Any suggestions?
EDIT
Separate into two tables?
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 |
You have to use function FIND_IN_SET :
SELECT * FROM table WHERE FIND_IN_SET('1', col2) > 0;
In case you gonna use separated table:
SELECT *
FROM table1
JOIN table2 ON table1.id = table2.f_id
WHERE table2.col2 = 1;
链接地址: http://www.djcxy.com/p/75338.html
上一篇: mysql:与max进行匹配并进行查询
下一篇: 选择所有具有特定号码的
