• <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根据逻辑在存储过程中动态构建查询字符串

        时间:2023-10-25

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

                  上一篇:在 R 中按组连接列 下一篇:ORA-00900: 无效的 SQL 语句 - 在 oracle 10g 中运行过程时

                  相关文章

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