Unix Technical Forum

Question about aliasing

This is a discussion on Question about aliasing within the MySQL forums, part of the Database Server Software category; --> Take this query I'm running from the mysql commandline: SELECT name, SUM(joined_0) AS J0, SUM(joined_1) AS J1, ABS(SUM(joined_0) - ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:47 AM
Stan R.
 
Posts: n/a
Default Question about aliasing

Take this query I'm running from the mysql commandline:

SELECT name,
SUM(joined_0) AS J0,
SUM(joined_1) AS J1,
ABS(SUM(joined_0) - SUM(joined_1)) AS J_Diff,
SUM(joined_0)/(SUM(joined_0) + SUM(joined_1))*100 AS J0_Pcnt,
SUM(joined_1)/(SUM(joined_0) + SUM(joined_1))*100 AS J1_Pcnt
FROM ps_people p,
ps_pdata d,
ps_pdata_etc e
WHERE d.dataid = e.dataid AND
d.pid = p.pid
GROUP BY d.plrid
HAVING J0 > 0 AND
J1 > 0
ORDER BY J_Diff ASC
LIMIT 20;




1) Why is it I can't use J0 and J1 in the definition for J_Diff and
J0_Pcnt, J1_Pcnt

2) Is there any way to do it that I'm missing here?

I guess mainly I want ot save typing and it seems really redundant and
error prone to write a query the way I wrote it, and aliasing seemed
like it would make things less cluttered but all I get is 'Unknown
Column'

Thanks for any help.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:47 AM
Tigger
 
Posts: n/a
Default Re: Question about aliasing

You can't reference aliases in the field list or Where clauses.

You can in the Having and Order By though.

Tigger


Stan R. wrote:
> Take this query I'm running from the mysql commandline:
>
> SELECT name,
> SUM(joined_0) AS J0,
> SUM(joined_1) AS J1,
> ABS(SUM(joined_0) - SUM(joined_1)) AS J_Diff,
> SUM(joined_0)/(SUM(joined_0) + SUM(joined_1))*100 AS J0_Pcnt,
> SUM(joined_1)/(SUM(joined_0) + SUM(joined_1))*100 AS J1_Pcnt
> FROM ps_people p,
> ps_pdata d,
> ps_pdata_etc e
> WHERE d.dataid = e.dataid AND
> d.pid = p.pid
> GROUP BY d.plrid
> HAVING J0 > 0 AND
> J1 > 0
> ORDER BY J_Diff ASC
> LIMIT 20;
>
>
>
>
> 1) Why is it I can't use J0 and J1 in the definition for J_Diff and
> J0_Pcnt, J1_Pcnt
>
> 2) Is there any way to do it that I'm missing here?
>
> I guess mainly I want ot save typing and it seems really redundant and
> error prone to write a query the way I wrote it, and aliasing seemed
> like it would make things less cluttered but all I get is 'Unknown
> Column'
>
> Thanks for any help.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 08:48 AM
Stan R.
 
Posts: n/a
Default Re: Question about aliasing

Tigger wrote:
>
> Stan R. wrote:
>> Take this query I'm running from the mysql commandline:
>>

[...]example
>> 1) Why is it I can't use J0 and J1 in the definition for J_Diff and
>> J0_Pcnt, J1_Pcnt
>>
>> 2) Is there any way to do it that I'm missing here?
>>
>> I guess mainly I want ot save typing and it seems really redundant
>> and error prone to write a query the way I wrote it, and aliasing
>> seemed like it would make things less cluttered but all I get is
>> 'Unknown Column'
>>
>> Thanks for any help.

>
> You can't reference aliases in the field list or Where clauses.
>
> You can in the Having and Order By though.
>
> Tigger


Thank you. I could swear other DB engines allowed this. I remember doing
that sort of thing in Oracle a couple years ago. It seems to make so
much sense to me, to eliminate redundancy. Actualyl if MYsql allowed
subqueries the way Oracle did (like in the FROM list) this wouldn't be
an issue at all

--
Stan
Merry Christmas


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 08:48 AM
Captain Paralytic
 
Posts: n/a
Default Re: Question about aliasing


Stan R. wrote:
> Actualyl if MYsql allowed
> subqueries the way Oracle did (like in the FROM list)


It does, although I usually use JOINs instead

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 08:48 AM
Geoff Muldoon
 
Posts: n/a
Default Re: Question about aliasing

paul_lautman@yahoo.com says...
>
> Stan R. wrote:
> > Actualyl if MYsql allowed
> > subqueries the way Oracle did (like in the FROM list)

>
> It does, although I usually use JOINs instead


Oracle's
WITH <subquery> SELECT
and
FROM <subquery>
allow you to do some things that simple ANSI joins just can't match.

Geoff M
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 08:51 AM
Captain Paralytic
 
Posts: n/a
Default Re: Question about aliasing


Geoff Muldoon wrote:

> paul_lautman@yahoo.com says...
> >
> > Stan R. wrote:
> > > Actualyl if MYsql allowed
> > > subqueries the way Oracle did (like in the FROM list)

> >
> > It does, although I usually use JOINs instead

>
> Oracle's
> WITH <subquery> SELECT
> and
> FROM <subquery>
> allow you to do some things that simple ANSI joins just can't match.
>
> Geoff M


Yes it does, but this question is about doing things in MySQL!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:55 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com