MySQL 拆分字符串然后 select

MySQL split string and then select

我已经在互联网上进行了搜索,因此我正在努力寻找解决方案,但找不到上述解决方案,所以...

我有一个接受一个参数的存储过程,my_str。如果 my_str 字符串中有空格(比方说 "now is the winter of our discontent"),我想遍历该字符串并生成一个 WHERE 子句。 IE if my_str = "now is the winter of our discontent", 我想生成如下内容:

SELECT id, title, author
FROM my_table
where (my_content like '%now%'
    or my_content like '%is%'
    OR my_content like '%the%'
    OR my_content like '%winter%'
    OR my_content like '%of%'
    OR my_content like '%our%'
    OR my_content like '%discontent%'
);

非常感谢任何指导。

创建 sql 字符串并使用 PREPARE

    SET @s = CONCAT("SELECT id, title, author FROM my_table where (my_content like '%", REPLACE($myStr, " ", "%' OR my_content LIKE '%"), "%');");
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

阅读此内容了解更多信息:How To have Dynamic SQL in MySQL Stored Procedure