在sql查询中将单行中的逗号值分开

时间:2017-08-21 03:29:56

标签: php mysql sql

我有这个表结构

 id     flim_name     flim_actor_name
 1      Diwalie       Shah Rukh Khan,Kajol,Varun Dhawan
 2      Mersal        vijay, samantha,kajol

我需要将表格转换为此结构

id     flim_name     Actor_name       Heroin_name  director_name
1       Diwalie      Shah Rukh Khan     Kajol       Varun Dhawan
2       Mersal       vijay              samantha       kajol

我需要sql查询来解决这个问题

2 个答案:

答案 0 :(得分:0)

对此使用内爆

离。

$arr = array(Shah Rukh Khan,Kajol,Varun Dhawan);
echo implode(" ",$arr);
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>"; 
echo implode("X",$arr);

输出继电器:

Shah Rukh Khan Kajol Varun Dhawan
Shah Rukh Khan+Kajol+Varun Dhawan
Shah Rukh Khan-Kajol-Varun Dhawan
Shah Rukh KhanXKajolXVarun Dhawan

答案 1 :(得分:0)

问题的答案是

Select id, flim_name,
substring_index(substring_index ( `flim_actor_name`,',',1 ), ',',-1)as Hero,
substring_index(substring_index ( `flim_actor_name`,',',2 ), ',', -1)as Heroin,  
substring_index(substring_index(`flim_actor_name`,",",3),',',-1) as director_name   
from flim_management_table

表格结构:

 id      flim_name     Hero             Heroin       director_name
  1       Diwalie      Shah Rukh Khan     Kajol       Varun Dhawan
  2       Mersal       vijay              samantha       kajol