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

      <legend id='191Tc'><style id='191Tc'><dir id='191Tc'><q id='191Tc'></q></dir></style></legend>
      <tfoot id='191Tc'></tfoot>
        • <bdo id='191Tc'></bdo><ul id='191Tc'></ul>

        如何遍历表的所有行?(MySQL)

        How can I loop through all rows of a table? (MySQL)(如何遍历表的所有行?(MySQL))
          • <i id='anIBT'><tr id='anIBT'><dt id='anIBT'><q id='anIBT'><span id='anIBT'><b id='anIBT'><form id='anIBT'><ins id='anIBT'></ins><ul id='anIBT'></ul><sub id='anIBT'></sub></form><legend id='anIBT'></legend><bdo id='anIBT'><pre id='anIBT'><center id='anIBT'></center></pre></bdo></b><th id='anIBT'></th></span></q></dt></tr></i><div id='anIBT'><tfoot id='anIBT'></tfoot><dl id='anIBT'><fieldset id='anIBT'></fieldset></dl></div>

              <bdo id='anIBT'></bdo><ul id='anIBT'></ul>
              <legend id='anIBT'><style id='anIBT'><dir id='anIBT'><q id='anIBT'></q></dir></style></legend>

            • <tfoot id='anIBT'></tfoot>
                <tbody id='anIBT'></tbody>

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

                  本文介绍了如何遍历表的所有行?(MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一张表 A,有一个主键 ID.

                  I have a table A and there is one primary key ID.

                  现在我想遍历 A 中的所有行.

                  Now I want to go through all rows in A.

                  我发现了类似对于 A 中的每条记录"这样的内容,但这似乎不是您在 MySQL 中的做法.

                  I found something like 'for each record in A', but this seems to be not how you do it in MySQL.

                  事情是针对每一行,我想取一个字段并将其转换,将其插入另一个表中,然后更新该行的某些字段.我可以将 select 部分和 insert 放入一个语句中,但我也不知道如何在其中获取更新.所以我想循环.为了练习,我不想使用 MySQL 以外的任何东西.

                  Thing is for each row I want to take a field and transform it, insert it into another table and then update some of the row's fields. I can put the select part and the insert into one statement, but I don't know how to get the update in there as well. So I want to loop. And for practice I don't want to use anything else than MySQL.

                  编辑

                  我希望能举个例子.

                  以及一个不需要放入程序的解决方案.

                  And a solution which does not need to be put into a procedure.

                  编辑 2

                  好吧,想想这个场景:

                  表 A 和 B,每个都有字段 ID 和 VAL.

                  Table A and B, each with fields ID and VAL.

                  现在这是我想要做的伪代码:

                  Now this is the pseudo code for what I want to do:

                  for(each row in A as rowA)
                  {
                    insert into B(ID, VAL) values(rowA[ID], rowA[VAL]);
                  }
                  

                  基本上是使用循环将 A 的内容复制到 B 中.

                  basically copying content of A into B using a loop.

                  (这只是一个简化的例子,当然你不会为此使用循环.)}

                  (this is just a simplified example, of course you wouldn't use a loop for this.) }

                  推荐答案

                  因为循环的建议暗示了对过程类型解决方案的请求.这是我的.

                  Since the suggestion of a loop implies the request for a procedure type solution. Here is mine.

                  对从表中提取的任何单个记录起作用的任何查询都可以包装在一个过程中,使其在表的每一行中运行,如下所示:

                  Any query which works on any single record taken from a table can be wrapped in a procedure to make it run through each row of a table like so:

                  首先删除具有相同名称的任何现有过程,然后更改分隔符,这样您的 SQL 就不会在您尝试编写过程时尝试运行每一行.

                  First delete any existing procedure with the same name, and change the delimiter so your SQL doesn't try to run each line as you're trying to write the procedure.

                  DROP PROCEDURE IF EXISTS ROWPERROW;
                  DELIMITER ;;
                  

                  然后这是按照您的示例的程序(为清楚起见,使用 table_A 和 table_B)

                  Then here's the procedure as per your example (table_A and table_B used for clarity)

                  CREATE PROCEDURE ROWPERROW()
                  BEGIN
                  DECLARE n INT DEFAULT 0;
                  DECLARE i INT DEFAULT 0;
                  SELECT COUNT(*) FROM table_A INTO n;
                  SET i=0;
                  WHILE i<n DO 
                    INSERT INTO table_B(ID, VAL) SELECT (ID, VAL) FROM table_A LIMIT i,1;
                    SET i = i + 1;
                  END WHILE;
                  End;
                  ;;
                  

                  然后不要忘记重置分隔符

                  Then dont forget to reset the delimiter

                  DELIMITER ;
                  

                  并运行新程序

                  CALL ROWPERROW();
                  

                  您可以在INSERT INTO"中为所欲为我只是从您的示例请求中复制的行.

                  You can do whatever you like at the "INSERT INTO" line which I simply copied from your example request.

                  请仔细注意INSERT INTO"这里使用的线反映了问题中的线.根据对此答案的评论,您需要确保您的查询对于您正在运行的 SQL 版本在语法上是正确的.

                  Note CAREFULLY that the "INSERT INTO" line used here mirrors the line in the question. As per the comments to this answer you need to ensure that your query is syntactically correct for which ever version of SQL you are running.

                  在您的 ID 字段递增并从 1 开始的简单情况下,示例中的行可能变为:

                  In the simple case where your ID field is incremented and starts at 1 the line in the example could become:

                  INSERT INTO table_B(ID, VAL) VALUES(ID, VAL) FROM table_A WHERE ID=i;
                  

                  替换SELECT COUNT"线与

                  Replacing the "SELECT COUNT" line with

                  SET n=10;
                  

                  将让您仅在 table_A 中的前 10 条记录上测试您的查询.

                  Will let you test your query on the first 10 record in table_A only.

                  最后一件事.这个过程也很容易在不同的表中嵌套,并且是我可以在一个表上执行一个过程的唯一方法,该过程将不同数量的记录从父表的每一行动态插入到一个新表中.

                  One last thing. This process is also very easy to nest across different tables and was the only way I could carry out a process on one table which dynamically inserted different numbers of records into a new table from each row of a parent table.

                  如果你需要它运行得更快,那么一定要尝试让它基于设置,如果不是,那么这很好.您也可以以游标形式重写上述内容,但它可能不会提高性能.例如:

                  If you need it to run faster then sure try to make it set based, if not then this is fine. You could also rewrite the above in cursor form but it may not improve performance. eg:

                  DROP PROCEDURE IF EXISTS cursor_ROWPERROW;
                  DELIMITER ;;
                  
                  CREATE PROCEDURE cursor_ROWPERROW()
                  BEGIN
                    DECLARE cursor_ID INT;
                    DECLARE cursor_VAL VARCHAR;
                    DECLARE done INT DEFAULT FALSE;
                    DECLARE cursor_i CURSOR FOR SELECT ID,VAL FROM table_A;
                    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
                    OPEN cursor_i;
                    read_loop: LOOP
                      FETCH cursor_i INTO cursor_ID, cursor_VAL;
                      IF done THEN
                        LEAVE read_loop;
                      END IF;
                      INSERT INTO table_B(ID, VAL) VALUES(cursor_ID, cursor_VAL);
                    END LOOP;
                    CLOSE cursor_i;
                  END;
                  ;;
                  

                  请记住将您将使用的变量声明为与查询表中的变量相同的类型.

                  Remember to declare the variables you will use as the same type as those from the queried tables.

                  我的建议是尽可能使用基于集合的查询,并且仅在必要时使用简单的循环或游标.

                  My advise is to go with setbased queries when you can, and only use simple loops or cursors if you have to.

                  这篇关于如何遍历表的所有行?(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                  <small id='33qjX'></small><noframes id='33qjX'>

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