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)
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)
解决方案 »
- my.ini配置出错Cann't connect to DB
- 求助!! 如何插入 文件 到 longtext type的列?
- mysql多表用别名的方式查询时,到底与用左联,右联查询区别在哪里?
- int应该属于sign的类型
- 关于mysql set字段类型的问题
- mysql 「Ungultige Zeigeroperation」 是什么错误?
- MYSQL怎样实现序列
- jsp and mysql ?tomcat and apache?中国人不会JSP????
- 新人请教--从table中获取符合的值更新
- mysql分表的问题
- 请问谁有pgsql性能的数据
- ubuntu中安装mysql-server-5.0
select fd_car,faultcode, faultcode,createTime from 你的查询
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表啊
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表啊:为什么?
第二个问题,summary表中 )faultcode(遍历id所对应的faultcode) createTime(遍历id所对应的createTime)
两个字段存放的数据不是上面那个查询查出来的,而是对于id所对应的数据
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要传入,只有用SP了summary表中faultcode与createTime都是要再用一个查询语句通过id查询出来的
还要通过其它查询来得出结果?比如上述ID 840、780,还要连接其它表?
[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表中
。
。
。
。
。
。
直到表结束这些都是自动的
840 61 6158 2010-09-03 02:07:31
780 61 6159 2010-09-03 02:07:29 上述结果中的ID与其它字段是否是对应的?
遍历id所对应的createTime:是什么意思 ?
没有明白
当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)
那遍历应该怎么实现啊?
insert into summary(fd_car,faultcode, ralatecode,createTime)
select fd_car,faultcode, faultcode,createTime from (你的SQL代码 ) a
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 也是一样的
Thank you
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
1、写SQL语句,SQL语句不正确?
2、插入表中,字段不能对应?
select * from table1 where id='1','2','3'————-'最后一个id'
表中有无唯一标识的字段?假设为ID
select ID from table1 order by id limit 1
select * from table1 order by id;你不带条件,那么不就是把所有ID全部遍历么?