zoukankan      html  css  js  c++  java
  • sql 中将一行数据分成多行显示

      如果一个表设计成多条数据显示在一行,而业务需求需要显示成多条数据并要求按某字段排序。这时候就可以考虑在sql中进行处理了。不知道问题描述的是否够清晰,下面来具体看看吧。

         原数据库中的表结构如下所示:

        

       如图所示,所有的数据都显示在这一行里面,我需要将其显示成多数据行并且按照点击次数降序排列。

       我是这样处理的:

       

    CREATE FUNCTION FUNC_NEWTABLE()
    RETURNS @TMP_TABLE TABLE
    (
         TITLE    VARCHAR(
    100),          --标题
         LINKURL  VARCHAR(
    200),          --链接地址
         [COUNT]  INT                    
    --统计数
    )
    AS
    BEGIN
          INSERT INTO @TMP_TABLE(TITLE,LINKURL,[COUNT])
          SELECT title1,link1,count1 from hotarticle
          union all
          SELECT title2,link2,count2 from hotarticle
          union all
          SELECT title3,link3,count3 from hotarticle
          union all
          SELECT title4,link4,count4 from hotarticle
          union all
          SELECT title5,link5,count5 from hotarticle
          union all
          SELECT title6,link6,count6 from hotarticle
          union all
          SELECT title7,link7,count7 from hotarticle
          union all
          SELECT title8,link8,count8 from hotarticle
          union all
          SELECT title9,link9,count9 from hotarticle
          union all
          SELECT title10,link10,count10 from hotarticle
    RETURN
    END
    GO
    Select 
    * from dbo.FUNC_NEWTABLE() order by [count] desc

    主要就是利用sql函数将数据存储在一个新的表结构中,然后从新的表结构中提取数据。原理比较简单,得到的信息显示如下:

      

  • 相关阅读:
    nmcli 命令的基本使用
    kvm虚拟机添加网卡
    Centos 7 系统详解
    Android 开发工具下载
    Android Studio升级3.6 Build窗口出现中文乱码问题解决方案
    Android 透明度alpha换算16进制
    python 安装: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    关于AndroidStudio 配置的默认路径的修改
    通过设置代理解决AndroidStudio无法下载gradle问题
    机器学习ROC图解读
  • 原文地址:https://www.cnblogs.com/luluping/p/1809918.html
Copyright © 2011-2022 走看看