If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Using variable in sql select

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-08-07, 15:38
stanimir_p stanimir_p is offline
Registered User
 
Join Date: Nov 2003
Posts: 12
Unhappy 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
Reply With Quote
  #2 (permalink)  
Old 02-08-07, 16:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-09-07, 03:10
stanimir_p stanimir_p is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 02-09-07, 04:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On