您的位置:程序门 -> .net技术 -> asp.net



这样的表结构,怎么以列表形式一行行显示出来


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


这样的表结构,怎么以列表形式一行行显示出来[已结贴,结贴人:majiming]
发表于:2007-06-25 10:15:28 楼主
表结构:
      (主键,递增)                 (标识)
              id                             code                   classname                   classvalue

              1                               100                     姓名                             马志远
              2                               100                     性别                             男
              3                               100                     年龄                             19

              4                               101                     姓名                             李志
              5                               101                     性别                             男
              6                               101                     年龄                             21

              7                               102                     姓名                             王强
              8                               102                     性别                             男
              9                               102                     年龄                             20                


请问我如何把上面的数据以表格的形式一行行显示出来.如下:


          姓名                                     性别                                       年龄
          ...                                       ...                                         ...
发表于:2007-06-25 10:19:431楼 得分:0
我的意思是说,sql语句应怎么写
发表于:2007-06-25 10:23:152楼 得分:5
没见过这样设计数据库的。
发表于:2007-06-25 10:23:433楼 得分:0
现在我确实有这样的需求
发表于:2007-06-25 10:27:324楼 得分:5
用程序来实现方便点

datatable   dt   =   new   ..

datacolumn   col   =   new   ..

dt.columns.add(col);

取数据加进来
发表于:2007-06-25 10:35:185楼 得分:0
晕了,我忘了.我是用asp写的
发表于:2007-06-25 10:36:396楼 得分:5
晕用asp更好办。用程序写出就得了呗。
发表于:2007-06-25 10:38:227楼 得分:5
那你还来.net   区
发表于:2007-06-25 10:39:498楼 得分:5
select   code   from   table  
select   *   from   table   where   code   =   ?
if   not   rs.eof   then
  rs( "姓名 ")
  rs( ".. ");
end   if
发表于:2007-06-25 10:40:099楼 得分:5
--测试数据
declare   @tb1   table(id   varchar(20),code   varchar(20),classname   varchar(20),classvalue   varchar(20))
insert   @tb1   values( '1 ', '1001 ', '姓名 ', '马志远 ')
insert   @tb1   values( '2 ', '1001 ', '性别 ', '男 ')
insert   @tb1   values( '3 ', '1001 ', '年龄 ', '19 ')
insert   @tb1   values( '4 ', '1002 ', '姓名 ', '李志 ')
insert   @tb1   values( '5 ', '1002 ', '性别 ', '男 ')
insert   @tb1   values( '6 ', '1002 ', '年龄 ', '21 ')

select   distinct   (select   classvalue   from   @tb1   where   code   =   a.code   and   classname   =   '姓名 ')   as   姓名,
(select   classvalue   from   @tb1   where   code   =   a.code   and   classname   =   '性别 ')   as   性别,
(select   classvalue   from   @tb1   where   code   =   a.code   and   classname   =   '年龄 ')   as   年龄
from   @tb1   a

---测试结果
李志 男 21
马志远 男 19
发表于:2007-06-25 10:41:1310楼 得分:5
--如果你的classname固定只有這三種,就很簡單


select
max(case   classname   when   n '姓名 '   then   classvalue   else   ' '   end)   as   姓名,
max(case   classname   when   n '性别 '   then   classvalue   else   ' '   end)   as   性别,
max(case   classname   when   n '年龄 '   then   classvalue   else   ' '   end)   as   年龄
from

group   by
code

--如果你的classname不是固定的,可能還有更多,就必須使用動態sql語句
发表于:2007-06-25 10:41:3411楼 得分:0
to:   lilopeng(糖球的魅力)  

具体怎么写呢
发表于:2007-06-25 10:42:3512楼 得分:0
不是固定的三条.有很多
发表于:2007-06-25 10:43:0913楼 得分:5
feiyu0805(飞雨)的可以得到結果,但是這裡可以不用子查詢的,子查詢的效率沒有直接group   by的效率高。
发表于:2007-06-25 10:46:3614楼 得分:0
楼上说的有理
发表于:2007-06-25 10:46:4115楼 得分:5
访问和更改关系数据     交叉数据报表 有时候需要旋转结果以便在水平方向显示列,而在垂直方向显示行。这就是所谓的创建   pivottable?、创建交叉数据报表或旋转数据。 假定有一个表   pivot,其中每季度占一行。对   pivot   的   select   操作在垂直方向上列出这些季度: year             quarter             amount ----             -------             ------ 1990             1                       1.1 1990             2                       1.2 1990             3                       1.3 1990             4                       1.4 1991             1                       2.1 1991             2                       2.2 1991             3                       2.3 1991             4                       2.4 生成报表的表必须是这样的,其中每年占一行,每个季度的数值显示在一个单独的列中,如: year   q1   q2   q3   q4   1990   1.1   1.2   1.3   1.4   1991   2.1   2.2   2.3   2.4   下面的语句用于创建   pivot   表并在其中填入第一个表中的数据: use   northwind go create   table   pivot (   year             smallint,     quarter       tinyint,       amount             decimal(2,1)   ) go insert   into   pivot   values   (1990,   1,   1.1) insert   into   pivot   values   (1990,   2,   1.2) insert   into   pivot   values   (1990,   3,   1.3) insert   into   pivot   values   (1990,   4,   1.4) insert   into   pivot   values   (1991,   1,   2.1) insert   into   pivot   values   (1991,   2,   2.2) insert   into   pivot   values   (1991,   3,   2.3) insert   into   pivot   values   (1991,   4,   2.4) go 下面是用于创建旋转结果的   select   语句: select   year,           sum(case   quarter   when   1   then   amount   else   0   end)   as   q1,         sum(case   quarter   when   2   then   amount   else   0   end)   as   q2,         sum(case   quarter   when   3   then   amount   else   0   end)   as   q3,         sum(case   quarter   when   4   then   amount   else   0   end)   as   q4 from   northwind.dbo.pivot group   by   year go 该   select   语句还处理其中每个季度占多行的表。group   by   语句将   pivot   中一年的所有行合并成一行输出。当执行分组操作时,sum   聚合中的   case   函数的应用方式是这样的:将每季度的   amount   值添加到结果集的适当列中,在其它季度的结果集列中添加   0。 如果该   select   语句的结果用作电子表格的输入,那么电子表格将很容易计算每年的合计。当从应用程序使用   select   时,可能更易于增强   select   语句来计算每年的合计。例如: select   p1.*,   (p1.q1   +   p1.q2   +   p1.q3   +   p1.q4)   as   yeartotal from   (select   year,                           sum(case   p.quarter   when   1   then   p.amount   else   0   end)   as   q1,                           sum(case   p.quarter   when   2   then   p.amount   else   0   end)   as   q2,                           sum(case   p.quarter   when   3   then   p.amount   else   0   end)   as   q3,                           sum(case   p.quarter   when   4   then   p.amount   else   0   end)   as   q4           from   pivot   as   p           group   by   p.year)   as   p1 go 带有   cube   的   group   by   和带有   rollup   的   group   by   都计算与本例显示相同的信息种类,但格式稍有不同。 请参见 select ?1988-2000   microsoft   corporation。保留所有权利。
发表于:2007-06-25 10:46:4716楼 得分:5
--如果你的classname固定只有三種

--創建測試環境
create   table   表
(id   int   identity(1,   1)   primary   key,
  code   char(3),
  classname   nvarchar(20),
  classvalue   nvarchar(20))
--插入數據
insert   表   select   '100 ',                     n '姓名 ',                             n '马志远 '
union   all   select   '100 ',                     n '性别 ',                             n '男 '
union   all   select   '100 ',                     n '年龄 ',                             n '19 '
union   all   select   '101 ',                     n '姓名 ',                             n '李志 '
union   all   select   '101 ',                     n '性别 ',                             n '男 '
union   all   select   '101 ',                     n '年龄 ',                             n '21 '
union   all   select   '102 ',                     n '姓名 ',                             n '王强 '
union   all   select   '102 ',                     n '性别 ',                             n '男 '
union   all   select   '102 ',                     n '年龄 ',                             n '20 '      
go
--測試
select
max(case   classname   when   n '姓名 '   then   classvalue   else   ' '   end)   as   姓名,
max(case   classname   when   n '性别 '   then   classvalue   else   ' '   end)   as   性别,
max(case   classname   when   n '年龄 '   then   classvalue   else   ' '   end)   as   年龄
from

group   by
code
go
--刪除測試環境
drop   table   表
--結果
/*
姓名 性别 年龄
马志远 男 19
李志 男 21
王强 男 20
*/

发表于:2007-06-25 10:47:0017楼 得分:5
rs=server.create( "adodb.recordset ")
控制   rs一条一条记录寻循环啊。。。

生成你要的表格。
发表于:2007-06-25 10:49:1618楼 得分:5
--准备数据
declare   @tab   table(
[id]   int
,code   int
,classname   nvarchar(10)
,classvalue   nvarchar(10)
)


insert   into   @tab
select   1,   100,   '姓名 ',   '马志远 '
union   all   select   2,   100,   '性别 ', '男 '
union   all   select   3,   100,   '年龄 ', '19 '
union   all   select   4,   101,   '姓名 ', '李志 '
union   all   select   5,   101,   '性别 ', '男 '
union   all   select   6,   101,   '年龄 ', '21 '
union   all   select   7,   102,   '姓名 ', '王强 '
union   all   select   8,   102,   '性别 ', '男 '
union   all   select   9,   102,   '年龄 ', '20 '


--原始表
select   *   from   @tab  

--查询
select   distinct   a.code
,b.classvalue   as   '姓名 '
,c.classvalue   as   '性别 '
,d.classvalue   as   '年龄 '
from   @tab   a
left   outer   join   @tab   b   on   b.classname= '姓名 '   and   a.code=b.code
left   outer   join   @tab   c   on   c.classname= '性别 '   and   a.code=c.code
left   outer   join   @tab   d   on   d.classname= '年龄 '   and   a.code=d.code


===结果
100 马志远 男 19
101 李志 男 21
102 王强 男 20
发表于:2007-06-25 10:51:2519楼 得分:5
--如果你的classname不是固定的
--使用動態sql語句,創建存儲過程實現
--創建存儲過程
create   procedure   sp_test
as
begin
declare   @s   nvarchar(4000)
select   @s   =   ' '
select   @s   =   @s   +   ',   max(case   classname   when   n ' ' '   +   classname   +   ' ' '   then   classvalue   else   ' ' ' '   end)   as   [ '   +   classname   +   '] '
from   表   group   by   classname   order   by   min(id)
select   @s   =   '   select   '   +   stuff(@s,   1,   1,   ' ')   +   n '   from   表   group   by   code '
EXEC(@s)
end
go
--調用
EXEC   sp_test
发表于:2007-06-25 10:51:4420楼 得分:5
--如果你的classname不是固定的

--創建測試環境
create   table   表
(id   int   identity(1,   1)   primary   key,
  code   char(3),
  classname   nvarchar(20),
  classvalue   nvarchar(20))
--插入數據
insert   表   select   '100 ',                     n '姓名 ',                             n '马志远 '
union   all   select   '100 ',                     n '性别 ',                             n '男 '
union   all   select   '100 ',                     n '年龄 ',                             n '19 '
union   all   select   '101 ',                     n '姓名 ',                             n '李志 '
union   all   select   '101 ',                     n '性别 ',                             n '男 '
union   all   select   '101 ',                     n '年龄 ',                             n '21 '
union   all   select   '102 ',                     n '姓名 ',                             n '王强 '
union   all   select   '102 ',                     n '性别 ',                             n '男 '
union   all   select   '102 ',                     n '年龄 ',                             n '20 '      
go
--使用動態sql語句,創建存儲過程實現
--創建存儲過程
create   procedure   sp_test
as
begin
declare   @s   nvarchar(4000)
select   @s   =   ' '
select   @s   =   @s   +   ',   max(case   classname   when   n ' ' '   +   classname   +   ' ' '   then   classvalue   else   ' ' ' '   end)   as   [ '   +   classname   +   '] '
from   表   group   by   classname   order   by   min(id)
select   @s   =   '   select   '   +   stuff(@s,   1,   1,   ' ')   +   n '   from   表   group   by   code '
EXEC(@s)
end
go
--測試
EXEC   sp_test
go
--刪除測試環境
drop   table   表
drop   procedure   sp_test
--結果
/*
姓名 性别 年龄
马志远 男 19
李志 男 21
王强 男 20
*/
发表于:2007-06-25 10:52:5521楼 得分:0
忘了说了.我的数据库是access的,
谢谢大家热心帮助,我还没看呢,我先看一下大家的回复
发表于:2007-06-25 10:55:0022楼 得分:5
access   有个交叉表,用交叉表就行了

发表于:2007-06-25 10:57:3323楼 得分:5
暈,我還以為是ms   sql。

access的寫法有很大區別,沒有case,改用iif。

另外,access中有直接轉交叉表的,你試下,或者直接去acccess版問吧。
发表于:2007-06-25 15:25:4824楼 得分:5
select   a.classvalue,   b.classvalue,c.classvalue  
from  
(select   *   from   table_name   where   classname= '姓名 ')   a,
(select   *   from   table_name   where   classname= '性别 ')   b,  
(select   *   from   table_name   where   classname= '年龄 ')   c
where   a.code=b.code   and   b.code=.code;
发表于:2007-06-25 15:52:3925楼 得分:5
我的意思是说,sql语句应怎么写

晕了,我忘了.我是用asp写的

忘了说了.我的数据库是access的,

=================
1。
lz   好好强啊,问题豆不会说清楚,比较健忘~

2。
asp   +   access   偶也全部忘光光了

2。
参考:
.net   动态交叉表
http://www.cnblogs.com/jinglecat/archive/2007/05/24/757919.html


发表于:2007-06-26 10:28:1326楼 得分:5
asp的话,自己循环生成表格就行了,不用这么麻烦的sql语句
发表于:2007-06-27 09:25:0627楼 得分:5
在程序里面调下吧!!!


快速检索

最新资讯
热门点击