将表更改为视图 - 表使用日期功能

时间:2014-01-04 03:08:16

标签: mysql

我有MySQL查询添加数据数据添加到reg_data表和session =早上,该查询运行.......运行此查询数据应该每天定期添加到表....由于使用当前日期功能。但如果我改变它以查看查询运行不好。这是我的原始代码

create  into velocity (date,run,velocity) 
select
  DATE_SUB(CURDATE(),INTERVAL 1 DAY),
  ((select anem_reading from reg_data where date=CURDATE() and session='morning')-(select anem_reading from reg_data where date=DATE_SUB(CURDATE(),INTERVAL 1 DAY)  and session='morning')) as run ,
  round((((select   anem_reading from reg_data where date=CURDATE() and session='morning')-(select anem_reading from reg_data where date= DATE_SUB(CURDATE(),INTERVAL 1 DAY) and session='morning')) /24 ),1) as velocity
from reg_data
where
  session='morning'
  and date=CURDATE();

我改变它创建视图

create  view velocity as (date,run,velocity)
select
  DATE_SUB(DATE(),INTERVAL 1 DAY),
  ((select anem_reading from reg_data where date=DATE() and session='morning')-(select anem_reading from reg_data where date=DATE_SUB(DATE(),INTERVAL 1 DAY) and session='morning')) as run ,
  round((((select   anem_reading from reg_data where date=DATE() and session='morning')-(select anem_reading from reg_data where date= DATE_SUB(DATE(),INTERVAL 1 DAY) and session='morning')) /24 ),1) as velocity
from reg_data
where
  session='morning'
  and date=DATE();

你能帮我解决这个问题吗?我想从reg_data表的现有数据中运行这个视图而不使用当前日期选项。

1 个答案:

答案 0 :(得分:0)

尝试此操作并查看是否要显示每天的所有日期,运行,速度

create  view velocity as 
select
  DATE_SUB(r.date,INTERVAL 1 DAY) as date,
  ((select anem_reading from reg_data where date=r.date and session='morning')-(select anem_reading from reg_data where date=DATE_SUB(r.date,INTERVAL 1 DAY)  and session='morning')) as run ,
  round((((select   anem_reading from reg_data where date=r.date and session='morning')-(select anem_reading from reg_data where date= DATE_SUB(r.date,INTERVAL 1 DAY) and session='morning')) /24 ),1) as velocity
from reg_data r
  WHERE session='morning';

这是sqlFiddle