title: Assign and use mysql user variables in select clause
body: <p>I have a table with EID as primary key, (it is a varchar and contains some details) and uploadtime as a datetime column. I wanted the result from this table as :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But the last column is always null. The SQL I used is:</p>
  
  <pre><code>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') ) &gt;= 100 
  GROUP BY machine
  </code></pre>
  
  <p><a href="http://sqlfiddle.com/#!2/6fa45/1" rel="nofollow">Here is the link for SQL Fiddle</a></p>
  
  <p>Where am I wrong? Please help. Thanks in advance.</p>