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

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

                  相关文档推荐

                  Accessing another user#39;s table within an Oracle Stored Procedure(在 Oracle 存储过程中访问另一个用户的表)
                  How to View Oracle Stored Procedure using SQLPlus?(如何使用 SQLPlus 查看 Oracle 存储过程?)
                  How to Pass Java List of Objects to Oracle Stored Procedure Using MyBatis?(如何使用 MyBatis 将 Java 对象列表传递给 Oracle 存储过程?)
                  Set the variable result, from query(设置变量结果,来自查询)
                  What is dynamic SQL?(什么是动态 SQL?)
                  Mysql - How to quit/exit from stored procedure(Mysql - 如何退出/退出存储过程)

                      <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>