MySql在字符串中查找子字符串

时间:2014-11-14 10:33:53

标签: mysql sql mysqli

在mysql中,我如何获取作为\ n分隔字符串的文本并输出包含某个子字符串的每一行

例如: DB:

product | keywords
-------------------
test    |  test\ntest1\ntest2\ntest3
test1   |  test\nblah\nblah
test2   |  tst\nblah\nblah
test3   |  testr\nblah\nblah

PHP:

$searchUserInput = (retrieve user input from POST);
print_r($searchUserInput); //"test";
$query = "SELECT * FROM products WHERE"; (How would I write the WHERE clause to pull product test and test1)

代码有点复杂,但这就是我想要做的事情

1 个答案:

答案 0 :(得分:3)

这应该有效

select * from your_table
where find_in_set('test', replace(keywords, '\n', ',')) > 0
or find_in_set('test1', replace(keywords, '\n', ',')) > 0

但实际上,通过更改表格设计并且不会在一个列中存储多个值,您会更好。