title: creating column alias using function in mysql?
body: <p>I have this table </p>
  
  <pre><code>sales(productid, sales_date)
  </code></pre>
  
  <p>I want to get the monthly sales count for last 12 month. </p>
  
  <p>How can I generate the month name on the column alias (without hard coding)?</p>
  
  <p>I tried this :-</p>
  
  <p><a href="http://sqlfiddle.com/#!2/ee777/9" rel="nofollow">http://sqlfiddle.com/#!2/ee777/9</a></p>
  
  <p>But I want the result table in this format</p>
  
  <pre><code>productid   Oct-12  Nov-12  Dec-12  Jan-13  Feb-13  Mar-13  Apr-13  May-13
  1           0       0       3       4       2       8       2       11      
  2           0       5       6       8       2       0       0       0   
  </code></pre>
  
  <p>I tried to give the column alias using function <code>DATE_FORMAT(sales_date, '%M')</code> but it is 
  syntax error.</p>
  
  <pre><code>select productid,sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 12 month),'%Y%m'),1,0)) 
  `12 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 11 month),'%Y%m'),1,0)) 
  `11 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 10 month),'%Y%m'),1,0)) 
  `10 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 9 month),'%Y%m'),1,0)) 
  `9 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 8 month),'%Y%m'),1,0)) 
  `8 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 7 month),'%Y%m'),1,0)) 
  `7 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 6 month),'%Y%m'),1,0)) 
  `6 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 5 month),'%Y%m'),1,0)) 
  `5 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 4 month),'%Y%m'),1,0)) 
  `4 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 3 month),'%Y%m'),1,0)) 
  `3 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 2 month),'%Y%m'),1,0)) 
  `2 Months Before`,
  sum(if(DATE_FORMAT(sales_date,'%Y%m')=
  DATE_FORMAT(date_sub(curdate(),interval 1 month),'%Y%m'),1,0)) 
  `1 Months Before`
  from sales
  group by productid;
  </code></pre>
  
  <p>How can I get month and year as alias (eg:- Oct-12) in place of <code>12 Months Before</code> , <code>11 Months Before</code> and so on.</p>