mysql SUBSTRING_INDEX 一种行列转换的思想

本文深入探讨了MySQL中的SUBSTRING_INDEX函数,解析其工作原理,并展示了如何利用该函数进行数据的行列转换,从而优化数据处理和查询效率。通过对实例的分析,读者将更好地理解和运用这一实用的数据库操作技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


-- --  行列转换
 SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  
 FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)
 where a.relation_ids like '%,%' and a.pay_no_arr is null ;
-- 
-- -- 去重
-- SELECT fccps.cost_pay_id,fccps.id, mm.verify_no,fccp.payment_document from 
-- fc_corms_cost_pay_site as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_cost_pay as fccp 
-- where fccps.id=mm.resultNAME and fccps.cost_pay_id = fccp.id GROUP BY  mm.verify_no,fccp.payment_document;
-- 
-- 
-- -- 拼接结果集
-- SELECT kk.verify_no,GROUP_CONCAT(kk.payment_document)
--   from (SELECT fccps.cost_pay_id,fccps.id, mm.verify_no,fccp.payment_document from 
-- fc_corms_cost_pay_site as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_cost_pay as fccp 
-- where fccps.id=mm.resultNAME and fccps.cost_pay_id = fccp.id GROUP BY  mm.verify_no,fccp.payment_document) as kk GROUP BY kk.verify_no;
-- 
-- 
-- -- charu
-- INSERT INTO test_ref_temp (verify_code, payno_arry)
-- SELECT kk.verify_no,GROUP_CONCAT(kk.payment_document)
--   from (SELECT fccps.cost_pay_id,fccps.id, mm.verify_no,fccp.payment_document from 
-- fc_corms_cost_pay_site as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_cost_pay as fccp 
-- where fccps.id=mm.resultNAME and fccps.cost_pay_id = fccp.id GROUP BY  mm.verify_no,fccp.payment_document) as kk GROUP BY kk.verify_no;


-- 
SELECT verify_code,payno_arry,GROUP_CONCAT(payno_arry) from test_ref_temp GROUP BY verify_code

-- UPDATE fc_corms_cost_verify as a, test_ref_temp as b set a.pay_no_arr = b.payno_arry where a.verify_no =b.verify_code and a.pay_no_arr is null and a.relation_ids like '%,%';
-- SELECT * from  fc_corms_cost_verify as a, test_ref_temp as b where a.verify_no =b.verify_code and a.pay_no_arr is null and a.relation_ids like '%,%';


SELECT * from  fc_corms_cost_verify as a where  a.pay_no_arr is null and a.relation_ids like '%,%';

-- INSERT INTO test_ref_temp (verify_code, payno_arry)
-- SELECT kk.verify_no,GROUP_CONCAT(kk.payment_document)
--  from (SELECT fccps.other_fee_apply_id,fccps.id, mm.verify_no,fccp.pay_no as payment_document  from 
-- fc_corms_other_fee_apply_rent_money as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_other_fee_apply as fccp 
-- where fccps.id=mm.resultNAME and fccps.other_fee_apply_id = fccp.id GROUP BY  mm.verify_no,fccp.pay_no) as kk GROUP BY kk.verify_no;



SELECT verify_code,GROUP_CONCAT(payno_arry)  as payno_arry from test_ref_temp GROUP BY verify_code;



 

MySQL SUBSTRING_INDEX() 函数是一种用于截取字符串的函数,它可以根据指定的分隔符将字符串分割成多个部分,并返回其中的一个或多个部分。该函数的语法如下: ``` SUBSTRING_INDEX(str,delim,count) ``` 其中,str表示要截取的字符串,delim表示分隔符,count表示要返回的部分的数量。如果count为正数,则返回从左边开始的第count个部分;如果count为负数,则返回从右边开始的第count个部分。如果count为0,则返回整个字符串。如果分隔符在字符串中不存在,则返回整个字符串。 举个例子,假设有一个字符串"www.mytestpage.info",我们可以使用SUBSTRING_INDEX()函数将其分割成两个部分,即"www.mytestpage"和"info",代码如下: ``` SELECT SUBSTRING_INDEX('www.mytestpage.info','.',1); -- 返回"www.mytestpage" SELECT SUBSTRING_INDEX('www.mytestpage.info','.',-1); -- 返回"info" ``` 另外,引用中提到了一个示例,即使用SUBSTRING_INDEX()函数将IP地址拆分成4个相应的八位字节。具体代码如下: ``` SELECT SUBSTRING_INDEX('192.168.1.100','.',1) AS first_byte, SUBSTRING_INDEX(SUBSTRING_INDEX('192.168.1.100','.',2),'.',-1) AS second_byte, SUBSTRING_INDEX(SUBSTRING_INDEX('192.168.1.100','.',3),'.',-1) AS third_byte, SUBSTRING_INDEX('192.168.1.100','.',-1) AS fourth_byte; ``` 该代码将IP地址"192.168.1.100"拆分成4个相应的八位字节,并将它们分别存储在first_byte、second_byte、third_byte和fourth_byte变量中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值