2013-04-29 142 views
1

我有下面的语法,我想用它来创建一个MySQL的看法:的MySQL创建视图与MySQL查询包含变量

create view `ViewName` as (select 
       v_starting.callingname, 
       v_starting.geofence, 
       v_starting.`updatetime`, 
       @lastGroup := @lastGroup + if(@lastAddress = v_starting.geofence 
             AND @lastVehicle = v_starting.callingname, 0, 1) as GroupSeq, 
       @lastVehicle := v_starting.callingname as justVarVehicleChange, 
       @lastAddress := v_starting.geofence as justVarAddressChange 
      from 
       v_starting, 
       (select @lastVehicle := '', 
         @lastAddress := '', 
         @lastGroup := 0) SQLVars 

      order by 
       v_starting.`updatetime`) 

这种失败,错误:

#1351 - View's SELECT contains a variable or parameter 

我怎样才能解决这个问题?太感谢了。

回答

3

由于CREATE VIEW Syntax下记载:

A view definition is subject to the following restrictions:

[ deletia ]

  • The SELECT statement cannot refer to system or user variables.
+0

谢谢eggyal,希望能有一个工作。赞赏。 – Smudger 2013-04-29 12:39:09

1

认为你需要做到这一点使用子查询计数的记录。

事情是这样的: -

SELECT v_starting.callingname, 
       v_starting.geofence, 
       v_starting.`updatetime`, 
       Sub1.PrevRecCount ASGroupSeq 
FROM v_starting 
INNER JOIN (SELECT a.updatetime, COUNT(DISTINCT b.callingname, b.geofence) AS PrevRecCount 
      FROM v_starting a 
      LEFT OUTER JOIN v_starting b 
      ON a.updatetime > b.updatetime 
      AND a.callingname != b.callingname 
      AND a.geofence != b.geofence 
      GROUP BY a.updatetime) Sub1 

不是100%肯定关于这个问题,我半信半疑你如何订购的东西目前得到您的计数。