我搜索了互联网和SO以寻找我正在尝试做的解决方案,但找不到解决方案......
我有一个存储过程接受一个参数my_str。如果my_str在字符串中有空格(让我们说“现在是我们不满的冬天”),我想迭代该字符串并生成一个WHERE子句。 IE如果my_str =“现在是我们不满的冬天”,我想生成如下内容:
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%'
);
非常感谢任何指导。
答案 0 :(得分:0)
创建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