Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL use column name alias without SELECTING

Tags:

alias

sql

select

I know I can specify a column alias like so:

SELECT stuff as mask

Is there a way I can specify a column alias without returning that column data in the result set? Essentially, I want to be able to make my query clean by doing:

SELECT doManipulationStuff(cleanCompactAlias)

Where

reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
like image 327
torquestomp Avatar asked Apr 23 '13 17:04

torquestomp


2 Answers

You can use a Common Table Expression (CTE) to create a subquery with aliases:

WITH clean_cte AS
(
  SELECT reallyLong.misleading.andAnnoying.columnName1 as cleanCompactAlias1,
    reallyLong.misleading.andAnnoying.columnName2 as cleanCompactAlias2
)
SELECT doManipulationStuff(cleanCompactAlias1), 
  doManipulationStuff(cleanCompactAlias2)

That way you can put all of your aliasing in the CTE and just forget about it when calling your functions. You still have to do the aliasing somewhere, but this at least keeps it out of your main query to make it a bit more readable.

like image 66
Jeff Rosenberg Avatar answered Sep 24 '22 09:09

Jeff Rosenberg


You could use a subquery:

select doManipulationStuff(cleanCompactAlias)
from (select t.*, reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
      . . .
     ) t
like image 25
Gordon Linoff Avatar answered Sep 21 '22 09:09

Gordon Linoff