MySQL substring_index 分割字符串

row1:

[email protected];

[email protected];

[email protected];

[email protected]

row2:

[email protected];

[email protected]

把同一個Column的字符串,通過分隔符分割后逐行插入數據庫。

substring_index 定位是位置的左边,或者右边。而不能准确定位字符,需要做判断来返回正确的结果。

SELECT * FROM temp_biz_email
WHERE email_to='ech.com.cn'

INSERT temp_biz_email2(cust_no,email,carbon) 
SELECT cust_no,
SUBSTRING_INDEX(email_to,';',1) 
AS to1,1 FROM `temp_biz_email`


INSERT temp_biz_email2(cust_no,email,carbon) 
SELECT * from (
SELECT cust_no,
IF (SUBSTRING_INDEX(email_to,';',1)= email_to,NULL,
SUBSTRING_INDEX(
SUBSTRING_INDEX(email_to,';',2),';',-1)) 
AS email_to,1
FROM `temp_biz_email`
) AS tab WHERE tab.email_to !=''

对于第一个匹配的位置,可能会出现空的情况,例如 ;;;;;,去除null

INSERT temp_biz_email2(cust_no,email,carbon) 
SELECT * from (
SELECT cust_no,SUBSTRING_INDEX(email_cc,';',1) 
AS email_cc,1 FROM `temp_biz_email`
) AS tab 

WHERE tab.email_cc !='' AND email_cc not LIKE '%@eaddeek.com%'

你可能感兴趣的:(数据库,mysql,substring_index,分割)