编写两个数据表,A表(station)为基础表,B表(datinfo)为数据信息表,存贮A表所记录站点的数据信息,包含信息发生的时间字段。A、B为一对多关系,即A表一个站点在B表中有多个时间(年份)的数据记录。
问题:需查询A表任意站点拥有数据记录的时间范围,可采用视图方式解决,但效率太低,可采取在A表中补充两个字段:起始时间(startyear)、终止时间(endyear)。
解决代码如下:
update station a set
startyear=(select min(year) from datinfo b where b.station=a.code),
endyear=(select max(year) from datinfo c where c.station=a.code)