for (char c = 'A'; c <= 'Z'; c++)在数据库中这条语句应该怎么写I23435
I43465
H34623
B32452
K43245
L35324我想输出结果是
I类 几本书
H类 几本书
K类 几本书
L类 几本书实际上有A-Z类书

解决方案 »

  1.   

    select left(col,1),
           count(*)
    from tb 
    group by left(col,1)
      

  2.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (col nvarchar(12))
    insert into [tb]
    select 'I23435' union all
    select 'I43465' union all
    select 'H34623' union all
    select 'B32452' union all
    select 'K43245' union all
    select 'L35324'
    select left(col,1)+N'類'類別,
           count(*)數量
    from tb 
    group by left(col,1)
    /*
    類別   數量
    ---- -----------
    B類   1
    H類   1
    I類   2
    K類   1
    L類   1(5 個資料列受到影響)
    */