Domanda

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"

È stato utile?

Soluzione

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

Altri suggerimenti

You can't reuse an alias in the select or where clause. Only in group, having, order clauses.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top