IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]MySQL一些SQL技巧

    liuzhoulong发表于 2016-06-21 18:07:34
    love 0
    1,一行转多行
          url列以分号分隔,将其一行转化为多行,借助自增长表help_topic 实现。
    select a.channel_id,channel_code,site_name,siteid,refer_channel,substring_index(substring_index(a.url,';',b.help_topic_id+1),';',-1) as urls from  t_channel_mapping a join mysql.help_topic b on (length(a.url) - length(replace(a.url,';',''))+1) > b.help_topic_id; 
    2,多行转一行
         分组后汇总成一行,orderid以逗号分隔
      select SiteID,group_concat(distinct cast(orderid as char(8))) as orderid from  site_order

    3,Mysql实现rownumber
        mysql是不支持rownum函数,下面例子实现产品表中给产品一个顺序编号

        select @rownum:=@rownum+1 rownum, product
        from 
       (
           select (@rownum:=0),a.product 
        from 
            (select  product from  t_proudct_info  GROUP BY product) a
        ) t



沪ICP备19023445号-2号
友情链接