博客
关于我
sql将列显示为行
阅读量:141 次
发布时间:2019-02-27

本文共 671 字,大约阅读时间需要 2 分钟。

写这篇文章的时候参考的是《sql hacks》,用的是sql server 2008

很多时候excel里面的表都是这样的:

姓名   高数   英语   数学

小明    89      56      67

小红     77     66     99

小米     89   

create  table  tbGradeIn (    student  varchar(10)  ,     gaoshu int ,     yingyu int ,     yuwen int      )insert into tbGradeIn(student ,  gaoshu ,  yingyu , yuwen )values ( '小明' , 89 , 56 , 67) , ('小红' ,  77 , 66 , 99 ) insert into  tbGradeIn (student , gaoshu)values('小米'  , 89)

导入数据库之后就变成了

姓名   高数   英语   数学

小明    89      56      67

小红     77     66     99

小米     89     null   null 

但是这样并不规范,应改成:

姓名   课程    分数

小明   高数    89

小明    英语    56

*****

就是将行显示为列!!下面有种解决方法:

select  student , '高数'  ,  gaoshu  from tbGradeIn union select  student , '英语' , yingyu from tbGradeInunion select student , '语文' , yuwen from tbGradeIn

转载地址:http://qofd.baihongyu.com/

你可能感兴趣的文章
Nginx 配置解析:从基础到高级应用指南
查看>>
Nginx 集成Zipkin服务链路追踪
查看>>
nginx 集群配置方式 静态文件处理
查看>>
Nginx+Django-Python+BPMN-JS的整合工作流实战项目
查看>>
Nginx+Keepalived+LVS集群实战
查看>>
Nginx+Keepalived实现简单版高可用主备切换
查看>>
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>