unable to use aliases in SELECT

This is a discussion on unable to use aliases in SELECT within the MySQL Database forums, part of the Database Forums category; Say I have the following table / rows: --------------------------------------- CREATE TABLE test ( stock_num VARCHAR(25) DEFAULT '', ordered INT(8) DEFAULT '0', received ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-13-2007
yawnmoth
 
Posts: n/a
Default unable to use aliases in SELECT

Say I have the following table / rows:

---------------------------------------

CREATE TABLE test (
stock_num VARCHAR(25) DEFAULT '',
ordered INT(8) DEFAULT '0',
received INT(8) NULL DEFAULT NULL
);

INSERT INTO test VALUES ('a', 2, 4);
INSERT INTO test (stock_num, ordered) VALUES ('a', 1);

---------------------------------------

Now say that I try to run the following:

---------------------------------------

SELECT stock_num,
ordered - COALESCE(received, 0) AS subtotal,
SUM(CASE
WHEN subtotal < 0 THEN 0
ELSE subtotal
END) AS total
FROM test
GROUP BY stock_num;

---------------------------------------

Why do I get a "Unknown column 'subtotal' in 'field list'" when I do
that? Replacing subtotal, in the CASE, with what it's actually equal
to yields the results that I want, but it seems less efficient... why
require MySQL do the math twice when it could, instead, do it once?

Reply With Quote
  #2 (permalink)  
Old 09-13-2007
Good Man
 
Posts: n/a
Default Re: unable to use aliases in SELECT

yawnmoth <terra1024@yahoo.com> wrote in news:1189712903.012872.235200@
50g2000hsm.googlegroups.com:

> Say I have the following table / rows:
>
> ---------------------------------------
>
> CREATE TABLE test (
> stock_num VARCHAR(25) DEFAULT '',
> ordered INT(8) DEFAULT '0',
> received INT(8) NULL DEFAULT NULL
> );
>
> INSERT INTO test VALUES ('a', 2, 4);
> INSERT INTO test (stock_num, ordered) VALUES ('a', 1);
>
> ---------------------------------------
>
> Now say that I try to run the following:
>
> ---------------------------------------
>
> SELECT stock_num,
> ordered - COALESCE(received, 0) AS subtotal,
> SUM(CASE
> WHEN subtotal < 0 THEN 0
> ELSE subtotal
> END) AS total
> FROM test
> GROUP BY stock_num;
>
> ---------------------------------------
>
> Why do I get a "Unknown column 'subtotal' in 'field list'" when I do
> that? Replacing subtotal, in the CASE, with what it's actually equal
> to yields the results that I want, but it seems less efficient... why
> require MySQL do the math twice when it could, instead, do it once?


What if you change it to

SELECT stock_num,
ordered - COALESCE(received, 0) AS subtotal,
SUM(CASE
WHEN ordered < 0 THEN 0
ELSE ordered
END) AS total
FROM test
GROUP BY stock_num;


Reply With Quote
  #3 (permalink)  
Old 09-13-2007
ZeldorBlat
 
Posts: n/a
Default Re: unable to use aliases in SELECT

On Sep 13, 3:48 pm, yawnmoth <terra1...@yahoo.com> wrote:
> Say I have the following table / rows:
>
> ---------------------------------------
>
> CREATE TABLE test (
> stock_num VARCHAR(25) DEFAULT '',
> ordered INT(8) DEFAULT '0',
> received INT(8) NULL DEFAULT NULL
> );
>
> INSERT INTO test VALUES ('a', 2, 4);
> INSERT INTO test (stock_num, ordered) VALUES ('a', 1);
>
> ---------------------------------------
>
> Now say that I try to run the following:
>
> ---------------------------------------
>
> SELECT stock_num,
> ordered - COALESCE(received, 0) AS subtotal,
> SUM(CASE
> WHEN subtotal < 0 THEN 0
> ELSE subtotal
> END) AS total
> FROM test
> GROUP BY stock_num;
>
> ---------------------------------------
>
> Why do I get a "Unknown column 'subtotal' in 'field list'" when I do
> that? Replacing subtotal, in the CASE, with what it's actually equal
> to yields the results that I want, but it seems less efficient... why
> require MySQL do the math twice when it could, instead, do it once?


How do you know that MySQL is doing the math twice? Have you looked
at the code?

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:59 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0