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 a calculated field in another caculation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-10, 16:00
barghavan2004 barghavan2004 is offline
Registered User
 
Join Date: Sep 2010
Posts: 4
using a calculated field in another caculation

Hi there,
I am new to mysql.
My DB has a table named tbl1 of 5 number-type fields : num1,num2,num3,num4,num5
my sql Query is as the following:

SELECT
num1,
num2,
num1+num2 AS num3,
(num3+10)*2 AS num4,
num4 / 100 AS num5,
ID
FROM tbl1

the problem is that the fields num4 and num5 return zero.WHat is wrong with my query? Please help.
Reply With Quote
  #2 (permalink)  
Old 09-27-10, 18:34
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi,

I suspect that you have entries in the table that do not contain numeric values but instead contain NULL's. This is a special case in SQL. For instance, 1+0 = 1, but 1+NULL = NULL (or undefined). If the NULL values are to be interpreted as 0 then you must explicitly state this in your query using the IFNULL function.

Also looking at your query you are calling the resulting rows with names that appear to belong to the different fields in the table. So it is not clear whether you are looking to reuse the previously calculated num3 i.e. (num1 + num2) or the value of the num3 field in the table. SQL will always use the field name and never the alias.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com

Last edited by it-iss.com; 09-27-10 at 18:44.
Reply With Quote
  #3 (permalink)  
Old 09-27-10, 19:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
change this --
Code:
SELECT num1
     , num2
     , num1+num2 AS num3
     , (num3+10)*2 AS num4
     , num4 / 100 AS num5
     , ID
  FROM tbl1
to this --
Code:
SELECT num1
     , num2
     , num1+num2 AS num3
     , (num1+num2+10)*2 AS num4
     , (num1+num2+10)*2 / 100 AS num5
     , ID
  FROM tbl1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 09-28-10, 03:02
barghavan2004 barghavan2004 is offline
Registered User
 
Join Date: Sep 2010
Posts: 4
you were a great help
it works now
but how can i use ifnull in the statement
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