Actually, Mauricio is trying to concatenate the string 'October_' (or
whatever) to the *alias* of the column name, not to the column name itself.
I tried something like:

@set prefix = 'October_';

select totals as concat(prefix, totals)
from mytable

but it didn't work. Apparently, you can't use string functions like concat()
with the column alias. (I also tried writing the concat() expression as:

concat(prefix, 'totals')

and

concat(prefix, "totals")

but that didn't work either.

If there is some other way that might work, I know he would love to hear
about it. (I'd be interested too, just for curiousity's sake.)

Rhino
----- Original Message ----- 
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Cc: "Gleb Paharenko" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>;
"MySql List" <[EMAIL PROTECTED]>
Sent: Tuesday, November 16, 2004 1:47 PM
Subject: Re: Naming columns dynamically


> If what you are looking for is a way to say:
>
> SET @columnName='mycolumn';
> SELECT @columnName FROM mytable;
>
> and have it be interpreted as the same query as:
>
> SELECT mycolumn FROM mytable;
>
> Then you are out of luck.  However, nothing says you can't build a SQL
> statement in your application dynamically. Just use the string
> concatenation features built into your development language and you are
> off and running.
>
> I looked for anything in the docs akin to the M$SQL command "exec" which
> will attempt to execute any statement passed into it as an argument but
> MySQL doesn't have that command yet. Prepared statements and stored
> procedures were as close as I could get. Sorry!
>
> Shawn Green
> Database Administrator
> Unimin Corporation - Spruce Pine
>
>
>
> [EMAIL PROTECTED] wrote on 11/16/2004 03:25:36 PM:
>
> > Thanks Gleb, but I cant use that because the query is run directly from
> > within an aplication.
> >
> > Of course there're should other ways to do that  but I want to know if
> > it is posible from within MySql 4.1 gamma.
> >
> > If someone does know the answer to this question please let me know
> > Just in case someone else reads this thread the question is:
> >
> >
> > Is it possible to use variables as column names ?
> >
> > like this :   Select field1 as @var
> >       from table1
> >
> >
> > Thanks again
> > Mauricio
> >
> > On Tue, 2004-11-16 at 05:38, Gleb Paharenko wrote:
> > > Hello, Mauricio.
> > > I've found something in comments at:
> > >   http://dev.mysql.com/doc/mysql/en/Problems_with_alias.html
> > > But I don't know if that will help you. Here it is:
> > >
> > > "Posted by Ravi Kumar on November 10 2004 9:21am
> > >
> > > The above tip can be extended to have variables in column names.
> > >
> > > For example:
> > > The columns in table1 are cyJan,cyFeb,....,cyDec
> > >
> > > To select the only column pertaining to current month:
> > >
> > > t.sql contains:
> > > set @a=left(date_format(now(),'%M'),3);
> > > select concat('select cy',@a,' from table1',';');
> > >
> > > Execute the following command:
> > > mysql.exe -BN -u %uname% -p"%pwd%" -h %host% -P%port% dbname < t.
> > sql > t1.sql
> > >
> > > In t1.sql, we'll have the required command and this can be wrapped
> inside
> > > a shell script."
> > >
> > >
> > >
> > > Mauricio Pellegrini <[EMAIL PROTECTED]> wrote:
> > > > Hi ,
> > > >
> > > > I need to do the following:
> > > > Given a certain variable for example  @var  I want to include the
> > > > contents of that variable as part of the name of a result column.
> > > >
> > > > I'll try to explain myself a bit more with this example
> > > >
> > > >
> > > >        Set @var = 'October_' ;
> > > >
> > > >        Select
> > > >
> > > >           Tot as 'Totals'  <-- Here I would like to include the
> contents
> > > > of @var
> > > >        From sales              So the resulting column name would be
> > > >                                'October_Totals'
> > > >
> > > > The reason for this is because I don't know the value of @var which
> is
> > > > typed by the operator at the time the query is run.
> > > >
> > > > Is there a way to concatenate the contens of a variable  and a
> column
> > > > alias at run time ?
> > > >
> > > > thanks for your ideas
> > > > Mauricio
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > > -- 
> > > For technical support contracts, goto
> https://order.mysql.com/?ref=ensita
> > > This email is sponsored by Ensita.NET http://www.ensita.net/
> > >    __  ___     ___ ____  __
> > >   /  |/  /_ __/ __/ __ \/ /    Gleb Paharenko
> > >  / /|_/ / // /\ \/ /_/ / /__   [EMAIL PROTECTED]
> > > /_/  /_/\_, /___/\___\_\___/   MySQL AB / Ensita.NET
> > >        <___/   www.mysql.com
> > >
> > >
> > >
> >
> >
> > -- 
> > MySQL General Mailing List
> > For list archives: http://lists.mysql.com/mysql
> > To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]
> >
>


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to