• <i id='8RQPP'><tr id='8RQPP'><dt id='8RQPP'><q id='8RQPP'><span id='8RQPP'><b id='8RQPP'><form id='8RQPP'><ins id='8RQPP'></ins><ul id='8RQPP'></ul><sub id='8RQPP'></sub></form><legend id='8RQPP'></legend><bdo id='8RQPP'><pre id='8RQPP'><center id='8RQPP'></center></pre></bdo></b><th id='8RQPP'></th></span></q></dt></tr></i><div id='8RQPP'><tfoot id='8RQPP'></tfoot><dl id='8RQPP'><fieldset id='8RQPP'></fieldset></dl></div>

      • <bdo id='8RQPP'></bdo><ul id='8RQPP'></ul>

      <small id='8RQPP'></small><noframes id='8RQPP'>

      <tfoot id='8RQPP'></tfoot>
      1. <legend id='8RQPP'><style id='8RQPP'><dir id='8RQPP'><q id='8RQPP'></q></dir></style></legend>
      2. Mysql根据逻辑在存储过程中动态构建查询字符串

        Mysql dynamically build query string in a stored procedure based on logic(Mysql根据逻辑在存储过程中动态构建查询字符串)

          <small id='Ib7Ad'></small><noframes id='Ib7Ad'>

            <tbody id='Ib7Ad'></tbody>
          <i id='Ib7Ad'><tr id='Ib7Ad'><dt id='Ib7Ad'><q id='Ib7Ad'><span id='Ib7Ad'><b id='Ib7Ad'><form id='Ib7Ad'><ins id='Ib7Ad'></ins><ul id='Ib7Ad'></ul><sub id='Ib7Ad'></sub></form><legend id='Ib7Ad'></legend><bdo id='Ib7Ad'><pre id='Ib7Ad'><center id='Ib7Ad'></center></pre></bdo></b><th id='Ib7Ad'></th></span></q></dt></tr></i><div id='Ib7Ad'><tfoot id='Ib7Ad'></tfoot><dl id='Ib7Ad'><fieldset id='Ib7Ad'></fieldset></dl></div>
        • <tfoot id='Ib7Ad'></tfoot><legend id='Ib7Ad'><style id='Ib7Ad'><dir id='Ib7Ad'><q id='Ib7Ad'></q></dir></style></legend>
          • <bdo id='Ib7Ad'></bdo><ul id='Ib7Ad'></ul>

                  本文介绍了Mysql根据逻辑在存储过程中动态构建查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  目标是根据输入变量更改 Mysql 存储过程中的查询字符串.

                  The objective is to alter a query string within a Mysql stored procedure based on input variables.

                  像这样:

                  CREATE DEFINER=`root`@`localhost` PROCEDURE `func`(type VARCHAR(15))
                  BEGIN
                      SET @type = type;
                  
                      -- Check for the sort parameter
                      if @type="asc" THEN
                          SET @sort = " order by name asc";
                      elseif @type="desc" THEN
                          SET @sort = " order by name desc";
                      else
                          SET @sort ="";
                      end if;
                  
                  SELECT id, name from table @sort;
                  
                  END    
                  

                  推荐答案

                  解决方案是使用execute, and concat:

                  The solution is to use execute, and concat:

                  CREATE DEFINER=`root`@`localhost` PROCEDURE `test`(input VARCHAR(15))
                  BEGIN
                  SET @input = input;
                  
                  if @input="asc" then
                      SET @sort = " order by ActivityLogKey asc";
                  elseif @input = "desc" then
                      SET @sort = " order by ActivityLogKey desc";
                  else
                      SET @sort ="";
                  end if;
                  
                  SET @query = CONCAT('select * from activitylog ',@sort,' limit 0, 5');
                  
                  PREPARE stmt FROM @query;
                  EXECUTE stmt;
                  DEALLOCATE PREPARE stmt;
                  
                  END
                  

                  这篇关于Mysql根据逻辑在存储过程中动态构建查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)

                      <legend id='MhgwF'><style id='MhgwF'><dir id='MhgwF'><q id='MhgwF'></q></dir></style></legend>

                      <small id='MhgwF'></small><noframes id='MhgwF'>

                      <tfoot id='MhgwF'></tfoot>
                      • <i id='MhgwF'><tr id='MhgwF'><dt id='MhgwF'><q id='MhgwF'><span id='MhgwF'><b id='MhgwF'><form id='MhgwF'><ins id='MhgwF'></ins><ul id='MhgwF'></ul><sub id='MhgwF'></sub></form><legend id='MhgwF'></legend><bdo id='MhgwF'><pre id='MhgwF'><center id='MhgwF'></center></pre></bdo></b><th id='MhgwF'></th></span></q></dt></tr></i><div id='MhgwF'><tfoot id='MhgwF'></tfoot><dl id='MhgwF'><fieldset id='MhgwF'></fieldset></dl></div>
                          <bdo id='MhgwF'></bdo><ul id='MhgwF'></ul>
                            <tbody id='MhgwF'></tbody>