比如表table1中

ID,A1,A2,A3,A4
这些字段
我要返回A1+A2+A3+A4的和最大top5的IDsql语句是怎么写的呢

解决方案 »

  1.   


    select id,sum(A1+A2+A3+A4) as total from table1 where 1 order by total desc limit 5;
      

  2.   

    #建立测试数据
    create table zy (id int,a1 int,a2 int,a3 int,a4 int)
    insert into zy values (101,1,2,3,4),(102,2,3,4,5),(103,1,8,9,3),(104,3,8,5,2),(105,11,23,2,10),(106,62,22,9,33)#查询
    select id
    from zy group by id  order by sum(a1+a2+a3+a4) desc limit 5#结果
    id      
    ----
    106
    105
    103
    104
    102