Search code examples
mysqllabelrenamealias

mysql rename column output query


mysql rename column output query

Hey guys! I'm trying to create a kind of alias to use as a variable in grafana but I'm not getting the desired result... My intention is that the query returns only the alias created from the query and not the name of the column itself. See my query below:

select dcontext as Recebidas from cdr_local where dcontext = 'inc_alvoko' group by dcontext;

With this query I get the answer as shown below:

+------------+
| Recebidas  |
+------------+
| inc_alvoko |
+------------+

Where is the column name "inc_alvoko" I want to rename it to "Received" but I can't...

I will greatly appreciate any member who can help me with this detail!


Solution

  • seems you are looking for a "translation" for your countents so you could try using case

    select distinct 
    case when dcontext =  'inc_alvoko' then 'Received' END  Recebidas 
    from cdr_local w
    where dcontext = 'inc_alvoko'
    

    and if you need distinct result but not use a ggregation function you should not use group by but use DISTINCT clause in select .

    the improper use of group bt without aggregation function produce error (by default) in mysql version starting by 5.7