2张表  一张作为历史记录  一张是临时数据表历史记录表字段
high,low,null,getinfotime临时表里有个字段都是数字,大于10的算high ;大于0小于10的算low; 等于0的算null原来我都是3个数值 用3个SQL算完  在手动插入到临时表 但现在需要 用一条SQL 就查出来
该怎么写?getinfotime 是查询另一张的最小时间  select min(getinfotime) as getinfotime from 表X

解决方案 »

  1.   

    真的看不懂你的需求
    “原来我都是3个数值 用3个SQL算完  在手动插入到临时表 ”
    建议把这些语句贴出来,以便分析
      

  2.   


    大致猜一下。select if(字段>10,字段,null),
    if(字段>0 and 字段<10,字段,null),
    if(字段=0,字段,null)
    from 临时数据表
      

  3.   

    原本是  select count(数值字段) from 临时表  where 数值>10  得出个数字 算作是high其他2个同上 分别算作 low  和 zero再从别的表取得日期 
    select min(getinfotime) as getinfotime from 别的表然后手动把 3个数字1个日期  插入到 历史记录表现在我需要1条SQL 把4个小查询并作1条SQL
      

  4.   

    继续猜!select (select min(getinfotime)  from 别的表) as getinfotime,
    sum(if(数值字段>10,1,0)) as high,
    sum(if(数值字段<10 and 数值字段>0,1,0)) as low,
    sum(if(数值字段00,1,0)) as `null`,
    from 临时表 楼主为什么不肯 "列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。",这样别人只能是永远猜测中回答你的问题。双方的效率都很差。
      

  5.   

    历史记录表
    id,high,low,null,getinfotime
    临时表
    id,num    num里放的就是数字其他表
    getinfotime
    另外算的是大于10的有多少  不是大于10的数字加起来有多少
      

  6.   

    insert into 历史记录表(high,low,`null`,getinfotime)
    select 
     sum(if(num>10,1,0)) as high,
     sum(if(num >0 and num<=10 ,1,0)) as low,
     sum(if(num=0,1,0)) as `null`,
     (select min(getinfotime) as getinfotime from 别的表) as getinfotime
    from 
    临时表;