论坛首页 入门技术论坛

行转列 sql

浏览 2326 次
锁定老帖子 主题:行转列 sql
该帖已经被评为新手帖
作者 正文
   发表时间:2007-08-09  

最近做的一个项目刚好要用到行转列的问题.由于以前没用写过这样的SQL语句,没办法只有到网上寻找答案,虽然能搜到相当多的结果,可仔细一看,都是列转行... 肺---气的都要炸了,全骗人..现在好不容易写出行转列的SQL 语句.希望对某些人有所帮助....


SELECT     sss
FROM         (SELECT     TOP (1) 1 AS Expr1, A AS sss
                       FROM          com_jjxx
                       UNION
                       SELECT     TOP (1) 2 AS Expr1,  B AS aaaa
                       FROM         com_jjxx AS com_jjxx_5
                       UNION
                       SELECT     TOP (1) 3 AS Expr1,  C AS bbb
                       FROM         com_jjxx AS com_jjxx_4
                       UNION
                       SELECT     TOP (1) 4 AS Expr1,  D AS ccc
                       FROM         com_jjxx AS com_jjxx_3
                       UNION
                       SELECT     TOP (1) 5 AS Expr1, E AS ddd
                       FROM         com_jjxx AS com_jjxx_2
                       UNION
                       SELECT     TOP (1) 6 AS Expr1, F  AS eeee
                       FROM         com_jjxx AS com_jjxx_1) AS a

 

有必要解释一下... 1,2,3,4,5,6, 就是就要显示的内容的一个排序..  A,B,C,D,E,F 就是要显示的内容.当然, com_jjxx 这个表里面要有这些字段.     com_jjxx  后面也可以加WHERE  条件...

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics