2013-04-08 52 views
0

我写了一个像这样的准备语句,工作正常。在mysql中准备一个Prepare语句并在其中使用convert_tz

BEGIN 
SET @tempaccountID :=CONCAT('\'',accountID1,'\''); 
SET @tempdeviceID := CONCAT('\'',deviceID1,'\''); 
SET @query :=CONCAT('select accountID, 
      deviceID, 
      FROM_UNIXTIME(timestamp) as timestamp, 
      statusCode, 
      latitude, 
      longitude, 
      speedKPH, 
      heading, 
      address, 
      odometerKM, 
      Charging 
      from ',(SELECT eventtableName FROM migration_run_time WHERE accountID=accountId1 AND deviceID=deviceID1), 
      ' where accountID=',@tempaccountID, 
      ' and deviceID=',@tempdeviceID, 
      ' and latitude!=0.0 and longitude!=0.0 and speedKPH<120 and timestamp= (select max(timestamp) from ', (SELECT eventtableName FROM migration_run_time WHERE accountID=accountId1 AND deviceID=deviceID1) , 
               ' where accountID=',@tempaccountID, 
               ' and deviceID=',@tempdeviceID, 
               ' and latitude!=0.0 and longitude!=0.0 and speedKPH<120);'); 


PREPARE stmt FROM @query; 
EXECUTE stmt; 

现在来大问题

我想用CONVERT_TZ在声明中获得的时间所需时间格式这样

convert_tz(FROM_UNIXTIME(timestamp),device.timeZone,device.requiredTimeZone) as timestamp 

//设备与另一个表在上面声明

如果我将以上述方式编写此代码,则必须在coloumn名称前缀中添加表名称,但事件表本身是在运行时计算的帐户ID和设备ID的基础..

我应该怎么做呢?

附:我在MySQL非常糟糕..我是一个> net/jQuery开发人员,并没有关于mysql的IDea。请帮助.. :(

回答

0

终于找到了解决办法...我创建了这个@timeZone和@requiredTimeZone两个变量..

谢谢@TOMBOM的帮助...

DELIMITER $$ 

USE `hiddenFromSO`$$ 

DROP PROCEDURE IF EXISTS `getLastCoordinate`$$ 

CREATE DEFINER=`root`@`%` PROCEDURE `getLastCoordinate`(accountID1 VARCHAR(32), 
         deviceID1 VARCHAR(64)) 
BEGIN 
    SET @tempaccountID :=CONCAT('\'',accountID1,'\''); 
    SET @tempdeviceID := CONCAT('\'',deviceID1,'\''); 

    SET @timeZone=CONCAT('\'',(SELECT timeZOne FROM device WHERE deviceID=deviceID1 AND accountID=accountID1 AND isActive='1'),'\''); 

    SET @requiredTimeZone=CONCAT('\'',(SELECT requiredTimeZone FROM device WHERE deviceID=deviceID1 AND accountID=accountID1 AND isActive='1'),'\''); 

    SET @query :=CONCAT('select accountID, 
       deviceID, 
       convert_tz(FROM_UNIXTIME(timestamp),',@timeZone,',',@requiredTimeZone,') as timestamp, 
       statusCode, 
       latitude, 
       longitude, 
       speedKPH, 
       heading, 
       address, 
       odometerKM, 
       Charging 
       from ',(SELECT eventtableName FROM migration_run_time WHERE accountID=accountId1 AND deviceID=deviceID1), 
       ' where accountID=',@tempaccountID, 
       ' and deviceID=',@tempdeviceID, 
       ' and latitude!=0.0 and longitude!=0.0 and speedKPH<120 and timestamp= (select max(timestamp) from ', (SELECT eventtableName FROM migration_run_time WHERE accountID=accountId1 AND deviceID=deviceID1) , 
                ' where accountID=',@tempaccountID, 
                ' and deviceID=',@tempdeviceID, 
                ' and latitude!=0.0 and longitude!=0.0 and speedKPH<120);'); 


    PREPARE stmt FROM @query; 
    -- select @query; 
    EXECUTE stmt; 
    END$$ 

DELIMITER ; 
1

我正要回答

通常这些问题是通过使用别名来解决。编写实际的表名后面的另一名和别名引用表。

但我觉得我第一次误解你的问题

不能从一个表,而from子句命名它在选择你将不得不做这样的事情:。

SET @query :=CONCAT(' 
select accountID, 
deviceID, 
convert_tz(FROM_UNIXTIME(timestamp),device.timeZone,device.requiredTimeZone) as timestamp 
statusCode, 
latitude, 
longitude, 
speedKPH, 
heading, 
address, 
odometerKM, 
Charging 
from ',(SELECT eventtableName FROM migration_run_time WHERE accountID=accountId1 AND deviceID=deviceID1), 
', device 
where accountID=',@tempaccountID, 
' and deviceID=',@tempdeviceID, 
' and latitude!=0.0 and longitude!=0.0 and speedKPH<120 and timestamp= (select max(timestamp) from ', (SELECT eventtableName FROM migration_run_time WHERE accountID=accountId1 AND deviceID=deviceID1) , 
' where accountID=',@tempaccountID, 
' and deviceID=',@tempdeviceID, 
' and latitude!=0.0 and longitude!=0.0 and speedKPH<120) 
and device.deviceID=deviceID1 
;'); 

这只是一个猜测,因为我不知道你的设备表是什么样的。

+0

感谢您的回答..让我试试看.. :) – writeToBhuwan 2013-04-08 09:05:08

+0

不工作..在这种情况下,Convert_tz正在退步null .. – writeToBhuwan 2013-04-08 11:09:06

+0

让我看看表格。 – fancyPants 2013-04-08 11:19:08