哪位高手能帮我看下这条sql语句的问题,为什么查询结果只能显示一列(即bjdj01)?select count(*) as bjdj01 from sb_ztb where sjdxzt='1' 
union 
select count(*) as bjdj02 from sb_ztb where sjdxzt='2' 
union 
select count(*) as bjdj03 from sb_ztb where sjdxzt='3'

解决方案 »

  1.   

    列名只取第一个select中的列名,你后两个as 别名是无效的
      

  2.   

    union ,取第一列的列名。
    要想都三个都出来,就不用union ,同时生成三个记录集
      

  3.   

    列名显示:bjdj01  没有问题.
    它的值是去掉了相同的记录.改为: UNION ALL 你就明白了.
      

  4.   

    create table sb_ztb
    (
    sjdxzt char(1)
    )
    insert into sb_ztb
    select 1 union all
    select 2 union all
    select 2 union all
    select 2 union all
    select 1 union all
    select 3select * from sb_ztbselect bjdj01=(select count(*) from sb_ztb where sjdxzt='1'),
           bjdj02=(select count(*) from sb_ztb where sjdxzt='2'),
           bjdj03=(select count(*) from sb_ztb where sjdxzt='3')
      

  5.   

    借用楼上的测试数据
    也可以用这种方式
    create table #sb_ztb
    (
    sjdxzt char(1)
    )
    insert into #sb_ztb
    select 1 union all
    select 2 union all
    select 2 union all
    select 2 union all
    select 1 union all
    select 3select * from #sb_ztbselect bjdj01,
           bjdj02,
           bjdj03
    from
    (select count(*) as bjdj01 from #sb_ztb where sjdxzt='1') as a,
    (select count(*) as bjdj02 from #sb_ztb where sjdxzt='2') as b,
    (select count(*) as bjdj03 from #sb_ztb where sjdxzt='3') as c