我有一个名为product.in的表,我将epfno存储为逗号sepatred值。但是现在我想要拆分epf no并插入一个具有相同data的单独行。我可以这样做吗?
示例
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
答案 0 :(得分:0)
执行以下操作:
foreach ($rows as $id => $row) {
$rows[$id] = current(explode(',', $row['Epfno']));
}
答案 1 :(得分:0)
要解决此问题,请创建一个如下所示的通用过程,并使用它将逗号分隔的字符串转换为行
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
之后通过调用它们在插入查询中使用该过程: - )