我需要在结果集中打印变量

时间:2013-12-17 08:10:30

标签: mysql

我需要能够将输入变量转换为日期时间或时间戳 - 然后将其作为select语句的一部分发送回结果集。我准备了下面的代码当然不起作用 - 无论unix时间是什么,它都会发送给我 1970-01-01 。这些是我试图发送到结果集的部分:

FROM_UNIXTIME(startDate,“%Y-%m-%d”)AS startingDate,     FROM_UNIXTIME(endDate,“%Y-%m-%d”)AS endingDate

CREATE PROCEDURE reportFreeCoolingTracker (
        IN fromDate varchar (50),
        IN toDate varchar (50),
        IN timeZone varchar (50))

    BEGIN
            DECLARE startDate varchar (50);
            DECLARE endDate varchar (50);


             SET startDate = FROM_UNIXTIME(fromDate/1000);
             SET endDate = FROM_UNIXTIME(toDate/1000);

 SELECT g.groupId,
    g.name AS groupName,
    g1.parentId AS parentId1, 
    g1.name AS group1Name,
    g2.parentId AS parentId2, 
    g2.name AS group2Name,
    g3.parentId AS parentId3,
    g3.name AS group3Name,
    l.logId,l.name AS logName,
    l.ordering AS logOrder,
    a.activityId AS activityId,
    a.ordering AS activityOrder,
    a.name AS activityName,
    l1.recordId,
    l1.started, l1.completed,
    l1.userId,l1.note,
    u.name,
    TO_SECONDS(t2.completed) - TO_SECONDS(l1.completed) AS timeInSeconds,
    substr(l.details, instr(l.details , ':' ) +1)AS charge,l.details,
    i.itemId,
    i.name AS itemName,
    i.itemType,
    i.details,l1.item31985,l1.item31987,
    (l1.item31985 - l1.item31987) AS kwDifference,
    ((l1.item31985 - l1.item31987) *  (substr(l.details, instr(l.details , ':' ) +1))) AS cost,
    (((l1.item31985 - l1.item31987) *  (substr(l.details, instr(l.details , ':' ) +1)))
    *(time_to_sec(timediff(t2.completed, l1.completed)) / 3600)) AS costT,
      time_to_sec(timediff(t2.completed, l1.completed)) / 3600 AS coolingHours,
      time_to_sec(timediff(endDate, startDate)) / 3600 AS totalTimeRange,
    FROM_UNIXTIME(startDate,"%Y-%m-%d") AS startingDate, 
    FROM_UNIXTIME(endDate,"%Y-%m-%d") AS endingDate

  FROM logs l 
      INNER JOIN groups g ON g.groupId = l.groupId
      LEFT JOIN groups g1 ON g.parentId = g1.groupId
      LEFT JOIN groups g2 ON g1.parentId = g2.groupId
      LEFT JOIN groups g3 ON g2.parentId = g3.groupId                
      INNER JOIN activities a ON l.logId = a.logId 
      INNER JOIN log1644 l1 ON a.activityId = l1.activityId 
      INNER JOIN log1644 t2 ON t2.recordId = l1.recordid + 1
      INNER JOIN items i ON l.logId = i.logId AND i.name LIKE '## KW%'
      INNER JOIN users u ON l1.userId = u.userId AND i.name LIKE '## KW%'
     WHERE i.itemID = "31985"  AND  l1.activityId = 1257
      AND l1.started
        BETWEEN startDate
            AND endDate
     ORDER BY l1.started;
    END //

DELIMITER ;

2 个答案:

答案 0 :(得分:0)

你必须删除

SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);

因为unix时间戳以毫秒为单位并在SELECT中用作

FROM_UNIXTIME(fromDate,"%Y-%m-%d") AS startingDate, 
FROM_UNIXTIME(toDate,"%Y-%m-%d") AS endingDate

答案 1 :(得分:0)

这实际上确实有效 - 实际上原始查询有效 - 我将它添加到错误的数据库中。

(CONVERT_TZ((FROM_UNIXTIME(fromDate / 1000)),'UTC',timeZone))AS startingDate,     (CONVERT_TZ((FROM_UNIXTIME(toDate / 1000)),'UTC',timeZone))AS endingDate,