Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Posts
    12

    Unhappy Unanswered: Using variable in sql select

    Hi
    Can I use select query that way:

    select a, b ,b*100/a as c, c-a/b, from mytable;
    how to use result "c" in the same query?

    in Microsoft Access it is possible but how to do with mysql -3.23 version
    if it is possible only in new version of mysql please let I know.
    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you can do this two ways

    one, just use the expression instead of the alias
    Code:
    select a, b ,b*100/a as c, b*100/a-a/b, from mytable
    two, upgrade to 4.1 to be able to use a subquery
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2003
    Posts
    12
    ofcource I can.
    select a, b ,b*100/a as c, b*100/a-a/b, from mytable
    that will work
    you know, the idea is when I want to use
    b*100/a many times as variable "c"
    and MySql should not to multiplay and divide every time


    select a, b ,b*100/a as c, c-a/b, c+d/a as e, e*100/b from mytable

    what is the way to use these variables "c" and "e" in mysql 4.1 or 5.0

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by stanimir_p
    what is the way to use these variables "c" and "e" in mysql 4.1 or 5.0
    Code:
    select a, b, c, c-a/b, e, e*100/b 
      from (
           select a, b, b*100/a as c, b*100/a+d/a as e
             from mytable
           ) as dt
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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