我有一個以EID作爲主鍵(它是一個varchar幷包含一些細節)和uploadtime作爲日期時間列的表。我想要的結果,從這個表:在select子句中分配和使用mysql用戶變量
substr(eid,5,5) as machine | count(eid) in group(grouped by machine) as packets | AvgDelay (as shown in SQL code) | packets * AvgDelay as Penalty
但最後一欄總是空。我使用的SQL是:
SELECT substr(eid, 5, 5) AS machine,
@packetcount: = count(*) AS packets,
@avg_delay: = round(avg(DATEDIFF(IF (STR_TO_DATE(uploadtime, '%Y-%m-%d') IS NULL, CURDATE(), STR_TO_DATE(uploadtime, '%Y-%m-%d')), STR_TO_DATE(SUBSTR(eid, 15, 8), '%Y%m%d')))) AS AvgDelay,
@packetcount * @avg_delay AS Penalty
FROM eidreport
WHERE uploadtime IS NOT NULL
OR DATEDIFF(IF (STR_TO_DATE(uploadtime, '%Y-%m-%d') IS NULL, CURDATE(), STR_TO_DATE(uploadtime, '%Y-%m-%d')), STR_TO_DATE(SUBSTR(eid, 15, 8), '%Y%m%d')) >= 100
GROUP BY machine
Here is the link for SQL Fiddle
我在哪裏錯了?請幫忙。提前致謝。