Sql代码如下select id,fd_car, faultcode,createTime from faultdaily
 where createTime 
between (select  date_sub(createTime,interval 60 second ) 
 from faultdaily where id='569' and fd_car=(select fd_car from faultdaily where id='569') 
)and (select  date_sub(createTime,interval -60 second ) 
 from faultdaily where id='569' and fd_car=(select fd_car from faultdaily where id='569')
)  
and faultcode<>(select faultcode from faultdaily where id='569')
/*
id      fd_car     faultcode     createTime
840      61         6158        2010-09-03 02:07:31
780      61         6159        2010-09-03 02:07:29 
或许会有很多值,也或许为null*/
要求是以id对这张表进行遍历(不知道表里面id从什么地方开始,也不知道什么地方结束),
还需要大家给我写一段Sql代码把遍历出来的数据存放到另一张表(summary)中。
summary表结构如下:
su_id(主键,自动增长)   fd_car(上面fd_car查询到的数据)faultcode(遍历id所对应的faultcode)      
 ralatecode(上面那段faultcode中的数据)     createTime(遍历id所对应的createTime)  

解决方案 »

  1.   

    贴要求结果出来看看insert into summary(fd_car,faultcode, ralatecode,createTime)
    select fd_car,faultcode, faultcode,createTime from 你的查询
      

  2.   

    上面那是两个问题,第一个问题是以主键Id进行遍历,对于id=‘569’的查询出来的结果就是
    id      fd_car     faultcode     createTime
    840      61         6158        2010-09-03 02:07:31
    780      61         6159        2010-09-03 02:07:29 第二个问题
    insert into summary(fd_car,faultcode, ralatecode,createTime)
    select fd_car,faultcode, faultcode,createTime from 你的查询
    对应不了我summary表啊
      

  3.   

    假设存为VIEW1:
    select id,fd_car, faultcode,createTime from faultdaily
     where createTime 
    between (select  date_sub(createTime,interval 60 second ) 
     from faultdaily where id='569' and fd_car=(select fd_car from faultdaily where id='569') 
    )and (select  date_sub(createTime,interval -60 second ) 
     from faultdaily where id='569' and fd_car=(select fd_car from faultdaily where id='569')
    )  
    and faultcode<>(select faultcode from faultdaily where id='569')
    insert into summary(fd_car,faultcode, ralatecode,createTime)
    select fd_car,faultcode, faultcode,createTime from view1对应不了我summary表啊:为什么?
      

  4.   

    第一个回答没看懂,存为视图?能说的详细一点吗?
    第二个问题,summary表中 )faultcode(遍历id所对应的faultcode)   createTime(遍历id所对应的createTime)  
    两个字段存放的数据不是上面那个查询查出来的,而是对于id所对应的数据 
      

  5.   

    两个字段存放的数据不是上面那个查询查出来的,而是对于id所对应的数据:什么意思?结果是什么?,已经查询出来的数据不能使用?详细说明create view view1 as 
    select id,fd_car, faultcode,createTime from faultdaily
     where createTime  
    between (select date_sub(createTime,interval 60 second )  
     from faultdaily where id='569' and fd_car=(select fd_car from faultdaily where id='569')  
    )and (select date_sub(createTime,interval -60 second )  
     from faultdaily where id='569' and fd_car=(select fd_car from faultdaily where id='569')
    )   
    and faultcode<>(select faultcode from faultdaily where id='569')
      

  6.   

    存为视图以后怎么遍历表?id=‘569’只是测试用的第二个问题我说的还不够详细:第一个查询目的是查询出某一个id的faultcode(故障代码)发生前后60S内发生的其他故障(faultcode,对应summary就是ralatecode),这样的故障叫做关联故障summary表中faultcode与createTime都是要再用一个查询语句通过id查询出来的
      

  7.   

    存为视图以后怎么遍历表?id=‘569’只是测试用的:
    如果ID要传入,只有用SP了summary表中faultcode与createTime都是要再用一个查询语句通过id查询出来的
    还要通过其它查询来得出结果?比如上述ID 840、780,还要连接其它表?
      

  8.   

    不要求传入,页面中方法执行这个sql语句的时候,就会把这个表从第一个id到最后一个id以上面sql语句中的条件进行遍历,并输出查询出来的数据,而第二个insert意思是上面sql语句中每执行一个id,下面的faultcode与createTime都是对应于这个id的。
    [code=SQL]su_id(主键,自动增长) 
    fd_car(上面fd_car查询到的数据)
    faultcode(遍历id所对应的faultcode)   
    ralatecode(上面那段faultcode中的数据) 
    createTime(遍历id所对应的createTime)
    ][/code   过程是
    id=1 查询出来数据
    把id为1的数据存放到summary表中
    id=2 查询出来数据
    把id为2的数据存放到summary表中






    直到表结束这些都是自动的
      

  9.   

    id      fd_car     faultcode     createTime
    840      61         6158        2010-09-03 02:07:31
    780      61         6159        2010-09-03 02:07:29 上述结果中的ID与其它字段是否是对应的?
    遍历id所对应的createTime:是什么意思 ?
    没有明白
      

  10.   

    第一个查询与summary中字段不对应
    当id=569时查询结果如下
    id fd_car faultcode createTime
    840 61 6158 2010-09-03 02:07:31
    780 61 6159 2010-09-03 02:07:29  
    summary表 存放的
    su_id(主键,自动增长) 
    fd_car(61
    faultcode(id=569对应的faultcode)   
     ralatecode(6158) 
    createTime(id=569对应的createTime) 
    那遍历应该怎么实现啊?
      

  11.   

    那就不明白了,为什么不能
    insert into summary(fd_car,faultcode, ralatecode,createTime)
    select fd_car,faultcode, faultcode,createTime from (你的SQL代码 ) a
      

  12.   

    id fd_car faultcode createTime
    840 61 6158 2010-09-03 02:07:31
    780 61 6159 2010-09-03 02:07:29   
    summary表 存放的
    su_id(主键,自动增长)  
    fd_car(61)
    faultcode(id=569对应的faultcode)   
     ralatecode(6158)  
    createTime(id=569对应的createTime) 
     
    insert into summary(fd_car,faultcode, ralatecode,createTime)
    select fd_car,faultcode, faultcode,createTime from (你的SQL代码 )首先把3段代码放在这
    第一:我的Sql代码查询出来faultcode的是id=840对应的,而我要求存放到summary里是id=569对应的faultcode。
    第二:createTime 也是一样的
      

  13.   

    这样说我想的的确是麻烦,那怎么修改第一个查询使之对应summary表,还有怎么遍历。。
    Thank you
      

  14.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  15.   

    是啊,到目前为止,我都还没有明白楼主的问题是什么:
    1、写SQL语句,SQL语句不正确?
    2、插入表中,字段不能对应?
      

  16.   

    那就简化吧,MySql怎么表进行一次遍历(id=1的时候查询出满足条件的数据,再执行id=2,查询出数据,最后吧数据集合起来)
    select * from table1 where id='1','2','3'————-'最后一个id'
      

  17.   

    select * from table1 where id in('1','2','3') order by id limit 1
      

  18.   

    只是针对20楼的问题
    表中有无唯一标识的字段?假设为ID
    select ID from table1 order by id limit 1
      

  19.   

    id 是主键,但select ID from table1 order by id limit 1只能查到id为1的数据
      

  20.   


    select * from table1 order by id;你不带条件,那么不就是把所有ID全部遍历么?
      

  21.   

    遍历?  每一句select不就是遍历吗?