4 messages in com.mysql.lists.win32RE: Query returns the WRONG result
FromSent OnAttachments
ascll07 May 2003 00:10 
Charles Mabbott07 May 2003 04:20 
Bria...@aol.com07 May 2003 10:57 
Bria...@aol.com07 May 2003 14:03 
Subject:RE: Query returns the WRONG result
From:Bria...@aol.com (Bria@aol.com)
Date:05/07/2003 02:03:12 PM
List:com.mysql.lists.win32

Try putting the sums from each table into a variable or temporary table:

SELECT @a:=sum(Amount_A) FROM Table_A; SELECT @b:=sum(Amount_B) FROM Table_B; SELECT @a+@b AS 'Total from both tables';

or

CREATE TEMPORARY TABLE temp (amt DECIMAL(10,2)); INSERT INTO temp (amt) SELECT sum(amount_a) FROM table_a; INSERT INTO temp (amt) SELECT sum(amount_b) FROM table_b; SELECT sum(amt) AS 'Total from both tables' FROM temp;

Either of the above queries should work in all circumstances. An aggregate function like 'sum()' won't work across tables.

-- Brian Richardson -- bria@aol.com -- http://brians.sytes.net/

--

Greetings,

Table_A ======= Field: Amount_A Value: 123.50 78.40 55.65 ( SUM( Table_A.Amount_A ) = 257.55 )

Table_B ======= Field: Amount_B Value: 258.50 250.40 651.90 ( SUM( Table_B.Amount_B ) = 1160.80 )

Question ======== How to I get the total amount from 2 tables?

I used this query: SELECT SUM( Table_A.Amount_A ) + SUM( Table_B.Amount_B ) FROM Table_A, Table_B; The result should be 1418.35, but I got is WRONG result. What wrong with my query?

Thanks in advance and I'm using MySQL ver 4.0.12

ascll asc@yahoo.com