MySQL错误代码:1349

时间:2017-07-17 07:23:45

标签: mysql

为什么这个查询可以在编译器中运行但在MySQL中不起作用并向我显示此错误。什么问题和任何解决方案?

Error Code: 1349. View's SELECT contains a subquery in the FROM clause  

示例表:

create table if not exists gpsdata
(ID int not null primary key, GPSLat varchar(20), GPSLon varchar(20));
insert into gpsdata values
(1 , 3.214502 , 101.638419),
(2 , 3.214478 , 101.638101);

我的查询

create view distances as
select
  prev.id prev_
, curr.id curr_
, prev.gpslat p_lat_
, prev.gpslon p_lon_
, curr.gpslat c_lat_
, curr.gpslon c_lon_
, ROUND( 6353 * 2 * 
    ASIN(SQRT( POWER(SIN((curr.GPSLat - abs(prev.GPSLat)) * pi()/180 / 2),2) 
  + COS(curr.GPSLat * pi()/180 ) * COS( abs(prev.GPSLat) *  pi()/180) 
  * POWER(SIN((curr.GPSLon - prev.GPSLon) * pi()/180 / 2), 2) ))
  , 2) as distance_km
from (
  select 
    id 
  , gpslat
  , gpslon
  from gps2u_infomation.gps_358899056115705
) prev join gps2u_infomation.gps_358899056115705 curr 
    on prev.id = curr.id - 1
where curr.id >= 1
;

1 个答案:

答案 0 :(得分:0)

FROM中无法在VIEW上使用SELECT

  

在MySQL 5.7.7之前,FROM语句不能在JOIN子句中包含子查询。
  来源: https://dev.mysql.com/doc/refman/5.7/en/create-view.html

在您的情况下,您只从表中选择一些数据(没有过滤器),因此您可以直接删除子选择和CREATE VIEW此表。请参阅以下CREATE VIEW distances AS SELECT prev.id prev_, curr.id curr_, prev.gpslat p_lat_, prev.gpslon p_lon_, curr.gpslat c_lat_, curr.gpslon c_lon_, ROUND( 6353 * 2 * ASIN(SQRT( POWER(SIN((curr.GPSLat - abs(prev.GPSLat)) * pi()/180 / 2),2) + COS(curr.GPSLat * pi()/180 ) * COS( abs(prev.GPSLat) * pi()/180) * POWER(SIN((curr.GPSLon - prev.GPSLon) * pi()/180 / 2), 2) )), 2) as distance_km FROM gps2u_infomation.gps_358899056115705 prev JOIN gps2u_infomation.gps_358899056115705 curr ON prev.id = curr.id - 1 WHERE curr.id >= 1; 声明:

{{1}}