查询结果集:
ps   online  totle
a     0       10
b     1       11
c     5        8
想显示成这样
ps      exp
a    (0)|(10) 
b    (1)|(11)
c    (5)|(8)如何写sql

解决方案 »

  1.   

    select 
      ps,
      '('ltrim(online)+')|('+ltrim(totle)+')' as exp
    from
      tb
      

  2.   

    select ps , exp = '(' + cast(online as varchar) + ')|(' + cast(total as varchar) + ')' from tb
      

  3.   

    select ps,exp='('+cast(online as varchar(10))+')|('+cast(totle as varchar(10))+')' from 表
      

  4.   


    --> 测试数据:@tb
    declare @tb table([ps] varchar(1),[online] int,[totle] int)
    insert @tb
    select 'a',0,10 union all
    select 'b',1,11 union all
    select 'c',5,8 
    select ps,exp='('+ltrim(online)+')'+'('+ltrim([totle])+')' from @tb/*
    ps   exp
    ---- ----------------------------
    a    (0)(10)
    b    (1)(11)
    c    (5)(8)(3 行受影响)
    */
      

  5.   

    select 
      ps,
      '('+ltrim(online)+')|('+ltrim(totle)+')' as [exp]
    from
      tb
    修改一楼
      

  6.   

    create table tb(ps varchar(10), online int, totle int)
    insert into tb values('a' ,   0 ,     10 )
    insert into tb values('b' ,   1 ,     11 )
    insert into tb values('c' ,   5 ,       8 )
    goselect ps , exp = '(' + cast(online as varchar) + ')|(' + cast(totle as varchar) + ')' from tbdrop table tb/*
    ps         exp                                                               
    ---------- ----------------------------------------------------------------- 
    a          (0)|(10)
    b          (1)|(11)
    c          (5)|(8)(所影响的行数为 3 行)
    */
      

  7.   

    select ps,exp='('+ltrim(online)+')|'+'('+ltrim([totle])+')' from @tb
      

  8.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ps] varchar(1),[online] int,[totle] int)
    insert [tb]
    select 'a',0,10 union all
    select 'b',1,11 union all
    select 'c',5,8select 
      ps,
      '('+ltrim([online])+')|('+ltrim([totle])+')' as exp
    from
      tb
    --测试结果:
    /*
    ps   exp
    ---- -----------------------------
    a    (0)|(10)
    b    (1)|(11)
    c    (5)|(8)(3 行受影响)*/
      

  9.   

    select ps,'('+ltrim(online)+')|('+ltrim(totle)+')' as exp  from  tb
     
      

  10.   


    declare @tb table([ps] varchar(1),[online] int,[totle] int)
    insert @tb
    select 'a',0,10 union all
    select 'b',1,11 union all
    select 'c',5,8
    select ps,
    reg = '(' + ltrim(online) +')|' + '(' + ltrim(totle) + ')' 
    from @tb