Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SELECT alias question

    I have a question regarding the mysql SELECT statement. Is it possible to use an alias in the expression for another alias, for example:

    Code:
    SELECT net,
    	tax_rate,
    	net / tax_rate AS tax,
    	net + tax AS gross
    FROM items
    But that dosent work, mysql dosent allow an alias in the SELECT statement (says it cant find a field called tax), which is kind of annoying as it means i have to duplicate all my calculations like this:

    Code:
    SELECT net,
    	tax_rate,
    	net / tax_rate AS tax,
    	net + (net / tax_rate) AS gross
    FROM items
    This might not seem like a big deal in this query, but i have some other far more complicated queries where its just a pain. Is there any way to do this, it seems like it should be possible.

    Thanks,
    Jack

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    While I understand why you might want to do this, SQL is usually the ~wrong~ place to be doing calculations, especially in a high load situation. The DB has limited resources and needs them all to serve data faster. Also, by adding columns to the returned row, your adding more data that needs to be sent over the tubes, when instead you could be offloading the calculation load to your application and save on both processing time on the DB and on data transfer, which reduces the amount of work you dump on the DB.

    And we all know that RDBMSs are not trucks. You can't dump on them.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •