Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql将有序列表拆分为多行_Mysql_Split_Delimiter_Preg Split - Fatal编程技术网

Mysql将有序列表拆分为多行

Mysql将有序列表拆分为多行,mysql,split,delimiter,preg-split,Mysql,Split,Delimiter,Preg Split,我有一个带有“指令”字段(mediumtext)的数据库,其中每个条目都以有序列表的形式包含一段指令。 当前,在查看时,通过使用PHPnl2br函数调用每个列表项,将其显示在新行上 示例条目: 将面粉、发酵粉和一撮盐放在碗中,然后混合。放在一边。2.将黄油和糖放在搅拌碗中,然后搅拌 用搅拌桨高速搅拌,直到变轻变奶油状 附件3.将混合器降至中速,然后逐渐添加 将鸡蛋充分乳化。4.加入面粉混合物,搅拌至均匀 它聚在一起形成面团。把面团从搅拌器中取出 将碗放入两张烤羊皮纸之间。5.擀面团 厚度为5毫米

我有一个带有“指令”字段(mediumtext)的数据库,其中每个条目都以有序列表的形式包含一段指令。 当前,在查看时,通过使用PHP
nl2br
函数调用每个列表项,将其显示在新行上

示例条目:

  • 将面粉、发酵粉和一撮盐放在碗中,然后混合。放在一边。2.将黄油和糖放在搅拌碗中,然后搅拌 用搅拌桨高速搅拌,直到变轻变奶油状 附件3.将混合器降至中速,然后逐渐添加 将鸡蛋充分乳化。4.加入面粉混合物,搅拌至均匀 它聚在一起形成面团。把面团从搅拌器中取出 将碗放入两张烤羊皮纸之间。5.擀面团 厚度为5毫米。6.在预热冷冻柜的同时,将其放入冷冻柜中 烘箱温度为170°C/340°F。7。剥去羊皮纸,烤面团 直到金黄。8.冷却,然后储存在密封容器中,直到 需要
  • 如您所见,文本中也有数字

    我想将这个字段拆分成一个单独的表,其中每个单独的指令列表项都有自己的行和id,将其链接到当前项


    有没有办法用MySQL拆分现有字段?可以使用“Number.”作为分隔符。

    您可以使用存储过程来完成此操作。这一个假设步骤从1开始,按顺序编号,所有步骤看起来都像步骤编号,后跟句点、空格,然后是步骤文本(这就是示例数据的外观)。它应该很容易修改,以使用稍微不同的格式。我已使程序生成步骤的结果集,但是您也可以将
    SELECT
    更改为
    INSERT
    ,以将步骤复制到新表中

    DELIMITER //
    DROP PROCEDURE IF EXISTS split_recipe //
    CREATE PROCEDURE split_recipe(IN recipe VARCHAR(2048))
    BEGIN
      DECLARE step INT DEFAULT 1;
      DECLARE next_step INT DEFAULT step+1;
      DECLARE this_step VARCHAR(256);
      WHILE recipe RLIKE CONCAT('^[[:blank:]]*', step, '[[.period.]]') DO
        -- is there a next step?
        IF recipe RLIKE CONCAT('^[[:blank:]]*', step, '[[.period.]] .*', next_step, '[[.period.]]') THEN
          SET this_step = SUBSTRING_INDEX(SUBSTRING_INDEX(recipe, CONCAT(next_step, '. '), 1), CONCAT(step, '. '), -1);
        ELSE
          SET this_step = SUBSTRING_INDEX(recipe, CONCAT(step, '. '), -1);
        END IF;
        -- output this step
        SELECT step, this_step;
        -- remove this step from the recipe
        SET recipe = SUBSTRING_INDEX(recipe, CONCAT(step, '. ', this_step), -1);
        SET step = next_step;
        SET next_step = step + 1;
      END WHILE;
    END //
    
    使用您的示例数据:

    CALL split_recipe('1. Place the flour, baking powder and a pinch of salt in a bowl and combine. Set aside. 2. Place the butter and sugar in a mixer bowl and cream at high speed until light and creamy, using the paddle attachment. 3. Reduce the mixer to a moderate speed and gradually add the egg until well emulsified. 4. Add the flour mixture and mix until it comes together to form a dough. Remove the dough from the mixing bowl and place between 2 sheets of baking parchment. 5. Roll the dough to a thickness of 5mm. 6. Place in the freezer while preheating the oven to 170°C/340°F. 7. Peel off the parchment and bake the dough until golden. 8. Allow to cool, then store in a sealed container until needed.')
    
    输出:

    step    this_step   
    1       Place the flour, baking powder and a pinch of salt in a bowl and combine. Set aside. 
    2       Place the butter and sugar in a mixer bowl and cream at high speed until light and creamy, using the paddle attachment. 
    3       Reduce the mixer to a moderate speed and gradually add the egg until well emulsified. 
    4       Add the flour mixture and mix until it comes together to form a dough. Remove the dough from the mixing bowl and place between 2 sheets of baking parchment. 
    5       Roll the dough to a thickness of 5mm. 
    6       Place in the freezer while preheating the oven to 170°C/340°F. 
    7       Peel off the parchment and bake the dough until golden. 
    8       Allow to cool, then store in a sealed container until needed.
    
    请注意,此过程生成多个单行结果集(每个步骤一个-为了便于阅读,我将它们组合在一起)。如果只需要一个结果集,则需要修改该过程以将步骤存储到临时表中,然后在最后从临时表中获取所有数据。或者,可以在应用程序中使用以下代码(对于PHP/PDO/MySQL):

    $result = $link->query("call split_recipe('1. Place the flour...')");
    do {
        if ($result->columnCount()) {
            $row = $result->fetch();
            print_r($row);
        }
    } while ($result->nextRowset());
    
    这是该过程的一个修改版本,它将从一个表
    配方(RecipeID INT,Instructions VARCHAR(2048))
    中将配方拆分为一个新表
    新配方(RecipeID INT,step_num INT,Instruction VARCHAR(256))


    您可以使用存储过程来实现这一点。这一个假设步骤从1开始,按顺序编号,所有步骤看起来都像步骤编号,后跟句点、空格,然后是步骤文本(这就是示例数据的外观)。它应该很容易修改,以使用稍微不同的格式。我已使程序生成步骤的结果集,但是您也可以将
    SELECT
    更改为
    INSERT
    ,以将步骤复制到新表中

    DELIMITER //
    DROP PROCEDURE IF EXISTS split_recipe //
    CREATE PROCEDURE split_recipe(IN recipe VARCHAR(2048))
    BEGIN
      DECLARE step INT DEFAULT 1;
      DECLARE next_step INT DEFAULT step+1;
      DECLARE this_step VARCHAR(256);
      WHILE recipe RLIKE CONCAT('^[[:blank:]]*', step, '[[.period.]]') DO
        -- is there a next step?
        IF recipe RLIKE CONCAT('^[[:blank:]]*', step, '[[.period.]] .*', next_step, '[[.period.]]') THEN
          SET this_step = SUBSTRING_INDEX(SUBSTRING_INDEX(recipe, CONCAT(next_step, '. '), 1), CONCAT(step, '. '), -1);
        ELSE
          SET this_step = SUBSTRING_INDEX(recipe, CONCAT(step, '. '), -1);
        END IF;
        -- output this step
        SELECT step, this_step;
        -- remove this step from the recipe
        SET recipe = SUBSTRING_INDEX(recipe, CONCAT(step, '. ', this_step), -1);
        SET step = next_step;
        SET next_step = step + 1;
      END WHILE;
    END //
    
    使用您的示例数据:

    CALL split_recipe('1. Place the flour, baking powder and a pinch of salt in a bowl and combine. Set aside. 2. Place the butter and sugar in a mixer bowl and cream at high speed until light and creamy, using the paddle attachment. 3. Reduce the mixer to a moderate speed and gradually add the egg until well emulsified. 4. Add the flour mixture and mix until it comes together to form a dough. Remove the dough from the mixing bowl and place between 2 sheets of baking parchment. 5. Roll the dough to a thickness of 5mm. 6. Place in the freezer while preheating the oven to 170°C/340°F. 7. Peel off the parchment and bake the dough until golden. 8. Allow to cool, then store in a sealed container until needed.')
    
    输出:

    step    this_step   
    1       Place the flour, baking powder and a pinch of salt in a bowl and combine. Set aside. 
    2       Place the butter and sugar in a mixer bowl and cream at high speed until light and creamy, using the paddle attachment. 
    3       Reduce the mixer to a moderate speed and gradually add the egg until well emulsified. 
    4       Add the flour mixture and mix until it comes together to form a dough. Remove the dough from the mixing bowl and place between 2 sheets of baking parchment. 
    5       Roll the dough to a thickness of 5mm. 
    6       Place in the freezer while preheating the oven to 170°C/340°F. 
    7       Peel off the parchment and bake the dough until golden. 
    8       Allow to cool, then store in a sealed container until needed.
    
    请注意,此过程生成多个单行结果集(每个步骤一个-为了便于阅读,我将它们组合在一起)。如果只需要一个结果集,则需要修改该过程以将步骤存储到临时表中,然后在最后从临时表中获取所有数据。或者,可以在应用程序中使用以下代码(对于PHP/PDO/MySQL):

    $result = $link->query("call split_recipe('1. Place the flour...')");
    do {
        if ($result->columnCount()) {
            $row = $result->fetch();
            print_r($row);
        }
    } while ($result->nextRowset());
    
    这是该过程的一个修改版本,它将从一个表
    配方(RecipeID INT,Instructions VARCHAR(2048))
    中将配方拆分为一个新表
    新配方(RecipeID INT,step_num INT,Instruction VARCHAR(256))


    这是一个起点:考虑规范化您的模式。在数字上进行拆分是不够好的-两张纸之间的
    170°C/340°F
    等文本将打破这种方法。你将需要一些更有趣的东西,比如NLP/上下文感知。(或者雇佣一个人手动完成这些步骤)你可以尝试建立一个类似数字(可能是多个数字)、点和一个空格的模式。根据您的数据,它可能需要一些手动修复。这种解析通常最好通过将数据选择到过程语言/客户端,在客户端对其进行解析,然后重新插入处理后的结果(并删除源)来完成。这可以在存储过程中完成,但非SQL语言通常对这类事情有更好的库支持。(MySQL甚至没有内置对“分隔符拆分”的支持,更不用说“一些数字”了,这可能一文不值。)这是一个起点:考虑规范化您的模式。数字拆分还不够好-两页之间的
    170°C/340°F
    等文本将打破这种方法。你将需要一些更有趣的东西,比如NLP/上下文感知。(或者雇佣一个人手动完成这些步骤)你可以尝试建立一个类似数字(可能是多个数字)、点和一个空格的模式。根据您的数据,它可能需要一些手动修复。这种解析通常最好通过将数据选择到过程语言/客户端,在客户端对其进行解析,然后重新插入处理后的结果(并删除源)来完成。这可以在存储过程中完成,但非SQL语言通常对这类事情有更好的库支持。(MySQL甚至没有内置的对“分隔符拆分”的支持,更不用说“一些数字”了,这可能毫无价值。)这会产生一个包含8行的结果集,还是8个单行结果?它会产生8个单行结果。如果只需要一个结果集,那么数据就需要进入一个临时表,以便在最后进行选择