php - MySQL 迭代分割字符串的元素并用它们做事

标签 php mysql stored-procedures

我的 MySQL 社区版 5.1 表中有一个单元格。内容始终是一定的数字。如果有多个数字,则由 ; 分号分隔。

例如:

| Column |
1ar8fj
99dkek;adjidk3;dajdid
divdae;dadjid;
NULL
dkadjcud;dasd;adfkvdo
dkjfakj
...

我需要编写一些代码,获取每个列值,通过 ; 将其拆分,然后使用拆分后的每个值执行另一个查询,并输出结果。

我知道我可以用 PHP 做到这一点,但我不需要将其制作成网页,所以我想知道这是否可以用 MySQL 语法编写? PHP 代码看起来像这样:

<?php
    $result = $mysqli->query('select column from table;');
    while ($row = $result->fetch_array($result)){
        $id_numbers = explode($row[0],';');
        foreach($id_numbers as $key => $val){
            // do another query
            $result2 = $mysqli->query('select * from table2 where col_val = "'.$val.'"');
            while ($row2 = $result2->fetch_array($result2){
                echo $row2[0].'<br>';
            }
        }
    }
?>

这可以直接用 MySQL 语法实现吗?

谢谢!!!

最佳答案

唷。好的。我终于让它工作了,但这里有一个作为存储过程的解决方案,它将字符串作为分隔符的输入,并在给定的名为 testtable

的表上运行
--Procedure: sprecursplit

--DROP PROCEDURE IF EXISTS sprecursplit;

DELIMITER |

CREATE PROCEDURE sprecursplit 
(
  IN delim nvarchar(255)
)
BEGIN
  declare tdone tinyint unsigned default(0);
  declare depth int unsigned default(1);
  declare datas nvarchar(255) default('');
  declare done tinyint unsigned default(0);
  declare dlength int unsigned default(1);
  declare hlength int unsigned default(0);
  declare pos int unsigned default(1);
  declare runpos int unsigned default(1);
  declare slice nvarchar(255) default('');

  drop table if exists allsubs;
  create temporary table allsubs(id int unsigned auto_increment, val nvarchar(255), primary key (id))engine = memory;

  while tdone <> 1 do
        if depth <= (select count(*) from testtable) then
           select t.datastring into datas from testtable t where t.id = depth limit 1;
           if length(datas) > 0 then
                set dlength = length(delim);
                set hlength = length(datas);
                set pos = 1;
                set runpos = 1;
                set slice = '';
                set done = 0;
                if hlength > 0 then
                   while done <> 1 do
                         if runpos > hlength then
                            set done = 1;
                         else
                             set pos = locate(delim, substring(datas from runpos));
                             if pos <> 1 then
                                if pos > 1 then
                                   set slice = substring(datas from runpos for (pos - 1));
                                else
                                    set slice = substring(datas from runpos);    
                                end if;
                                insert into allsubs (val) values (slice);
                             end if;
                             if pos = 0 then
                                 set runpos = runpos + hlength;
                             else
                                 set runpos = runpos + pos + dlength - 1;
                             end if;
                         end if;
                   end while;
                end if;
           end if;
           set depth = depth + 1;
        else
            set tdone = 1;
        end if;
  end while;
  select * from allsubs;
  drop table allsubs;
END|

DELIMITER ;

关于php - MySQL 迭代分割字符串的元素并用它们做事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11691942/

相关文章:

php - 当在另一个方法中返回 View 时,Laravel 返回空白页

php - 在 PHP 中测试变量是否为数字的正确方法是什么?

php - 如何在wordpress中使用命名空间,同时使用它的根文件类

mysql - 如何使用mysql获取用户的顶层分层数据?

php - 我们应该使用 eloquent 的 count 方法吗?

mysql - 远程 mySQL 连接从 XAMPP 抛出 "cannot connect to MySQL 4.1+ using the old insecure authentication"错误

mysql - mysql 如何对具有相同值的行进行排序?

php - 使用一个 html 选项向 mysql 数据库提交多个值

mysql - 如何修复 "Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation ' ='"

c# - 调用同一个 sql server 2005 存储过程的多个 asp.net 请求