Ich muss die Variable in der Ergebnismenge drucken

Ich muss in der Lage sein, eine Eingabevariable in eine Datums- oder Zeitmarke umzuwandeln - und dann an die Ergebnismenge als Teil der Select-Anweisung zurückzusenden. Ich habe den unten stehenden Code vorbereitet, der natürlich nicht funktioniert - er sendet mir 1970-01-01 unabhängig davon, was die Unix-Zeit ist. Dies sind die Teile, die ich an die Ergebnismenge senden möchte:

FROM_UNIXTIME (Startdatum, "% Y-% m-% d") AS Startdatum,     FROM_UNIXTIME (Enddatum, "% Y-% m-% d") AS Enddatum

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 (Startdatum, "% Y-% m-% d") AS Startdatum,
    FROM_UNIXTIME (Enddatum, "% Y-% m-% d") AS Enddatum

  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 ;
0

2 Antworten

Das funktioniert tatsächlich - tatsächlich funktionierte die ursprüngliche Abfrage - ich fügte es der falschen Datenbank hinzu.

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

0
hinzugefügt

Sie müssen entfernen

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

weil Unix Timestamp mit Millisekunden arbeitet und in Ihrem SELECT als verwendet

FROM_UNIXTIME(fromDate,"%Y-%m-%d") AS startingDate, 
FROM_UNIXTIME(toDate,"%Y-%m-%d") AS endingDate
0
hinzugefügt