Search code examples
mysqlaliasderived-table

How to use aliases in mysql functions


How do i use aliases with spaces in mathematical functions and group by statements. I am able to use it if I use single word aliases, but is there any way of achieving it if the name has spaces ?

select 
   count(date_format(start_date, '%W')) AS "NUMBER OF DAYS",
   repeat('*', "NUMBER OF DAYS") 
from    ABC

I tried using double-quotes,singe-quotes and also backticks(`), but it gives an error saying "Unknown column "NUMBER OF DAYS" in field list"


Solution

  • You can use Derived table

    select
    `NUMBER OF DAYS`,
    repeat('*', `NUMBER OF DAYS`) 
    from
    (
    select 
       count(date_format(start_date, '%W')) AS `NUMBER OF DAYS`
    from    ABC
    ) as t