0

我已经写了一个这样的准备语句并且工作正常。

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

// device是上面语句中的另一个表

如果我以上述方式编写,我必须在列名的 fromt 中为表名添加前缀,但事件表本身是在运行时根据 accountID 和 deviceID 计算的。

我应该如何处理它...

PS 我在 mysql 方面做得很差。我是一个 >net/jQuery 开发人员,对 mysql 没有任何想法。请帮忙.. :(

4

2 回答 2

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
;');

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

于 2013-04-08T08:52:04.060 回答
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 ;
于 2013-04-08T12:37:30.017 回答