我创建了以下字符串:
set l_project_name = concat("UPPER(a.project_name) like UPPER ('%",projectName,"%')");
set l_project_type = concat(" OR UPPER(a.project_type) like UPPER('%",projectType,"%')");
set l_project_description = concat(" OR UPPER(a.project_description) like UPPER('%",projectDescription,"%')");
set l_full_search_clause = concat(l_project_name ,l_project_type, l_project_description);
字符串的结果:l_full_search_clause是: UPPER(a.project_name)类似于UPPER('%order%')
我有一个select子句,我需要使用我创建的包含此where子句的字符串。所以,我有以下内容: 从项目中选择* 其中l_full_search_clause
但是,当然,MYSQL按原样读取l_full_search_clause而不是变量。我如何告诉MYSQL条件是变量字符串?
这个sql是在一个MYSQL存储过程..所以它只是简单的sql ..我从C#,MVC3调用这个存储过程
答案 0 :(得分:1)
试试这个:
SET @query = "SELECT * FROM projects WHERE ";
SET @l_project_name = CONCAT("a.project_name LIKE '%", projectName, "%'");
SET @l_project_type = CONCAT(" OR a.project_type LIKE '%", projectType, "%'");
SET @l_project_description = CONCAT(" OR a.project_description LIKE '%", projectDescription, "%'");
SET @l_full_search_clause = CONCAT(@l_project_name, @l_project_type, @l_project_description);
SET @query = CONCAT(@query, @l_full_search_clause);
PREPARE stmt FROM @query;
EXECUTE stmt;
我相信你必须对变量使用@var
语法。此外,要执行存储在字符串中的查询,您必须使用PREPARE
和EXECUTE
。
您不必在字符串上使用UPPER
。默认情况下,MySQL不区分大小写地比较字符串。您可以通过查看字段的排序规则类型来确保这一点。 ci_
代表“不区分大小写”。