我需要能够将输入变量转换为日期时间或时间戳-然后将其作为选择语句的一部分发送回结果集。我已经准备了下面的代码,但这当然不起作用-不管unix时间是多少,它都会向我发送1970-01-01。这些是我要发送给结果集的片段:

FROM_UNIXTIME(startDate,“%Y-%m-%d”)AS起始日期,
    FROM_UNIXTIME(endDate,“%Y-%m-%d”)AS结束日期

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 ;

最佳答案

您必须删除

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

关于mysql - 我需要在结果集中打印变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20628990/

10-16 17:20