t_customers 表中的一條記錄:
需要的結果:
sql實現
SELECT a.id, a.username, SUBSTRING_INDEX( SUBSTRING_INDEX( a.operid, ';', b.help_topic_id + 1 ), ';',- 1 ) AS ids FROM `t_customers` AS a JOIN mysql.help_topic AS b ON b.help_topic_id < ( length( a.operid ) - length( REPLACE ( a.operid, ';', '' ) ) + 1 );
如mysql.help_topic: help_topic_id 共有504個數值 它們是mysql內部的連續數列表,連續數列的最大值一定要大於符合分割的值的個數。
------------------------------------------------------------------------------------------------------------------------------------
mysql 字段截取函數:
1、left(字段名,index) 從左邊開始第index開始截取
2、right(字段名,index)從右邊開始第index開始截取
3、substring(字段名,index)當index>0從左邊開始截取直到結束 當index<0從右邊開始截取直到結束 當index=0返回空
4、substring(字段名,index,len)從index開始,截取len長度
5、substring_index(字段名,str,count),str是截取的字段 count是從哪里開始截取(0從左邊第0個開始,-1從右邊第一個開始)