我建立一个查询语句
select name+''+male+''+age from db 显示为张三 男 20怎么才可以才可以实现同一个单元格排成这样的格式呢?如下张三

20就是输出姓名后换行,然后输出年龄后换行,然后再输出年龄,,请高手赐教!不胜感激!!

解决方案 »

  1.   

    select name from tb
    union all
    select male from tb
    union all
    select age from tb
      

  2.   

    select name from tb 
    union all 
    select sex from tb 
    union all 
    select cast(age as varchar) from tb结果:
    张三

    20
      

  3.   

    如果为了达到楼主的要求,这样试试吧:
    select distinct name from tb where [name]='张三' 
    union all 
    select distinct sex from tb  where [name]='张三' 
    union all 
    select distinct age from tb where [name]='张三' 
      

  4.   

    如果为了更好的达到楼主的要求,应该这样试试: 
    select [列名] from [表名] where name ='张三' 
    GO 
    select [列名] from [表名]  where name ='张三' 
    GO
    select [列名] from [表名] where name='张三' 
    GO
      

  5.   

    print 'a'+char(13)+'b'a
    b或 ctrl+t
    select name+''+male+''+age from db 
      

  6.   

     ctrl+t 文本显示
    select name+char(10)+male+char(13)+ltrim(age) from db 
      

  7.   


    --> --> (Ben)生成測試數據
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb(id int,[Name] nvarchar(2),[sex] nvarchar(1),[age] int)
    Insert tb
    select 1,N'张三',N'男',20
    union all select 2,N'张四',N'男',20
    union all select 3,N'王五',N'女',20
    Go
    Select * from tb
    第一种
    select id,name from tb 
    union all 
    select id,sex from tb 
    union all 
    select id,cast(age as varchar) from tb1 张三
    2 张四
    3 王五
    1 男
    2 男
    3 女
    1 20
    2 20
    3 20第二种:
    select id,name
    from
    (
    select id,name from tb 
    union all 
    select id,sex from tb 
    union all 
    select id,cast(age as varchar) from tb ) p
    order by id 1 张三
    1 男
    1 20
    2 20  --->>这块为什么是年龄在前面,有谁知道吗?
    2 男
    2 张四
    3 王五
    3 女
    3 20