zoukankan      html  css  js  c++  java
  • Mysql中字段分割字符串( 一行转多行 )

    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从右边第一个开始)

  • 相关阅读:
    BZOJ1042: [HAOI2008]硬币购物
    BZOJ1089: [SCOI2003]严格n元树
    BZOJ1060: [ZJOI2007]时态同步
    BZOJ2697: 特技飞行
    BZOJ2464: 中山市选[2009]小明的游戏
    BZOJ1430: 小猴打架
    BZOJ3675: [Apio2014]序列分割
    BZOJ2453: 维护队列
    BZOJ2120: 数颜色
    BZOJ4547: Hdu5171 小奇的集合
  • 原文地址:https://www.cnblogs.com/JMrLi/p/11320273.html
Copyright © 2011-2022 走看看