select sum(num) from table where substring(id,1,3)='123'

解决方案 »

  1.   

    select sum(num) as sumNum from tablename where id like '123%'
      

  2.   

    id         num   name 
    12356    1       l
    12357    2       a
    12358    3       b
    12360    4       e
    45661    5       t
    45662    6       r
    45663    7       m
    45664    8      h那我还要显示象“123%”中name最小的列
      

  3.   

    select min(name) from tab where id like '123%'
      

  4.   

    select min(name) from tab where id like '123%' and name like '%somestring%'
      

  5.   

    select sum(num)
    from a
    where substring(cast(id as varchar),1,3)='123'substring函数是不能作用在整数类型上,所以要进行类型转换。
      

  6.   

    --drop table aa
    create table aa(id int,num int,name varchar(10))
    insert aa select 12356,1,'1'
    union all select 12357,2,'a'
    union all select 12358,3,'b'
    union all select 12360,4,'e'
    union all select 45661,5,'t'
    union all select 45662,6,'r'
    union all select 45663,7,'m'
    union all select 45664,8,'n'
    union all select 12364,9,'n'select sum(num)
    from aa
    where substring(cast(id as varchar),1,3)='123'
    drop table aa