Php 如何拆分逗号分隔的值并插入为具有相同数据的新行
我有一个名为product的表。在该表中,我将epfno存储为逗号sepatred值。但现在我希望拆分epf no并将其插入为具有相同数据的单独行。1如何才能做到这一点 范例Php 如何拆分逗号分隔的值并插入为具有相同数据的新行,php,mysql,Php,Mysql,我有一个名为product的表。在该表中,我将epfno存储为逗号sepatred值。但现在我希望拆分epf no并将其插入为具有相同数据的单独行。1如何才能做到这一点 范例 Tag No Epfno Department Weight In Time Out Time 82922 D1292,D1132 other2 36.03 5/2/2016 8:18 5/2/2016 8:18 82879 D1258,D1048 other2 30.36
Tag No Epfno Department Weight In Time Out Time
82922 D1292,D1132 other2 36.03 5/2/2016 8:18 5/2/2016 8:18
82879 D1258,D1048 other2 30.36 5/2/2016 8:26 5/2/2016 8:26
82883 D1225,D1256 other2 36.48 5/2/2016 8:28 5/2/2016 8:28
我想要的是
82922 D1292 other2 36.03 5/2/2016 8:18 5/2/2016 8:18
82922 D1132 other2 36.03 5/2/2016 8:18 5/2/2016 8:18
请执行以下操作:
foreach ($rows as $id => $row) {
$rows[$id] = current(explode(',', $row['Epfno']));
}
为了解决这个问题。。创建如下所示的通用过程,并使用该过程将逗号分隔的字符串转换为行
CREATE PROCEDURE `sp_split_string_into_rows`(in string_value varchar(2056))
BEGIN
-- Temporary table to hold splited row value of string
drop temporary table if exists temp_convert;
create temporary table temp_convert(split_data varchar(2056) );
-- dynamic query to break comma separated string as rows and insert into column
set @sql = concat("
insert into temp_convert (split_data)
values ('", replace(
( select group_concat(string_value) as converted_data), ",", "'),('"),"'
);"
);
prepare stmt1 from @sql;
execute stmt1;
END
之后,在insert查询中使用该过程,调用它们:-)这不是将值存储在DB中的最佳方法,最好先将数据库正常化,否则会在数据库中遇到问题future@Thamilan-看起来这正是海报想要做的-你想直接在mysql或使用php中这样做吗?询问谷歌“mysql将列字符串拆分为行”