Garayed.com  

Go Back   Garayed.com > mySQL
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-2007, 02:57 PM
Devon_Dan
 
Posts: n/a
Default Add values from multiple tables

This could be really simple...but I just can't seem to work it out so
any help is gratefully received.

I have several tables of equal dimensions, each full of numbers. What
I am trying to do is create a new table that presents combined totals.

========
Table One
========
1 | 2 | 3
--------
4 | 5 | 6

========
Table Two
========
0 | 2 | 4
--------
6 | 8 | 0

The result would be

1 | 4 | 7
----------------
10 | 13 | 6

Sure this is easy so if someone could point me in the right direction,
that would be great.

Dan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-21-2007, 04:03 PM
ZeldorBlat
 
Posts: n/a
Default Re: Add values from multiple tables

On Jul 21, 10:57 am, Devon_Dan <dp_pea...@hotmail.com> wrote:
> This could be really simple...but I just can't seem to work it out so
> any help is gratefully received.
>
> I have several tables of equal dimensions, each full of numbers. What
> I am trying to do is create a new table that presents combined totals.
>
> ========
> Table One
> ========
> 1 | 2 | 3
> --------
> 4 | 5 | 6
>
> ========
> Table Two
> ========
> 0 | 2 | 4
> --------
> 6 | 8 | 0
>
> The result would be
>
> 1 | 4 | 7
> ----------------
> 10 | 13 | 6
>
> Sure this is easy so if someone could point me in the right direction,
> that would be great.
>
> Dan


The problem is that you don't seem to have any condition on which to
join the two tables. In other words, given a row in Table One, how do
I know which row in Table Two to add to it? Hint: the "row number" is
not the right answer since there really isn't such a concept without
specifying a column to sort by.

Once you figure that out it should be trivial. Suppose the two tables
have a common field called "id." Your query would then look like
this:

select t1.a + t2.a as a,
t1.b + t2.b as b,
t1.c + t2.c as c
from table1 t1
join table2 t2
on t1.id = t2.id

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:06 AM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.