Converting delimited string to multiple values in mysql

后端 未结 5 1813
逝去的感伤
逝去的感伤 2020-12-11 18:40

I have a mysql legacy table which contains an client identifier and a list of items, the latter as a comma-delimited string. E.g. \"xyz001\", \"foo,bar,baz\". T

5条回答
  •  离开以前
    2020-12-11 18:59

    I really wanted to get this to work using SQL but was unable to. I wrote this quick PHP script to do the job.

    It's quick and dirty and NOT how I would ever recommend doing production code. But sometimes you do what it takes to get the job done quick.

    ".mysql_error();
        exit;
    }
    
    $applications = array();
    while($row = mysql_fetch_array($result)){
        echo "
    ".$row[$pri_column]; $topics = explode(",", $row[$column]); foreach($topics as $topic){ $topic = trim($topic); $applications[$row[$pri_column]][$topic] = $topic; } echo "
    ".$row[$column]; } echo "
    ";
    print_r($applications);
    echo "
    "; foreach($applications as $app => $topics){ foreach($topics as $topic){ $query = "insert into ".$newTable." values ('', \"".$app."\", \"".$topic."\")"; echo "
    ".$query; mysql_query($query); if(mysql_errno()){ echo "
    ".mysql_error(); } } } ?>

提交回复
热议问题