如何使用MySQL视图或过程模拟Explode()函数
我有一个MySQL表,其中的数据如下所示如何使用MySQL视图或过程模拟Explode()函数,mysql,sql,list,view,explode,Mysql,Sql,List,View,Explode,我有一个MySQL表,其中的数据如下所示 Struct : Id | Field N°1 Data : 1 | "File1.doc, File1.xls, File1.pdf" 2 | "File2.doc, File2.xls, File2.pdf" 感谢MySQL视图,我想要得到什么: 1 | "File1.doc" 1 | "File2.xls" 1 | "File1.pdf" 2 | "File2.doc" 2 | "File1.xls" 2 | "File2.pdf" 有
Struct :
Id | Field N°1
Data :
1 | "File1.doc, File1.xls, File1.pdf"
2 | "File2.doc, File2.xls, File2.pdf"
感谢MySQL视图,我想要得到什么:
1 | "File1.doc"
1 | "File2.xls"
1 | "File1.pdf"
2 | "File2.doc"
2 | "File1.xls"
2 | "File2.pdf"
有人知道吗?我不想用Php脚本来做,我需要用SQL来做。你能指定语言吗?还将其添加为标记。规范化数据模型。您不应该在一列中存储多个值。很抱歉,我没有指定它,但我的数据在MySql表中,我希望通过一个MySql视图获得结果,而不是通过php脚本。这会有所不同:)
<?php
$data = <<<STR
1 | "File1.doc, File1.xls, File1.pdf"
2 | "File2.doc, File2.xls, File2.pdf"
STR;
$rows = explode("\n", $data);
$result = array();
foreach ($rows as $row)
{
$num = $row[0];
$str = substr($row, 5, strlen($row) - 6);
$files = explode(', ', $str);
foreach ($files as $file)
{
$result[] = $num.' | "'.$file.'"';
}
}
foreach ($result as $file)
{
echo '<p>'.$file.'</p>';
}
?>
1 | "File1.doc"
1 | "File1.xls"
1 | "File1.pdf"
2 | "File2.doc"
2 | "File2.xls"
2 | "File2.pdf"