Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Result from query

Status
Not open for further replies.

venim

Technical User
Jul 19, 2004
14
BE
Hello,

I have a query which gives a weird result. The query is:

SELECT SUM(t1.hits) AS tab1, SUM(t2.hits) AS tab2
FROM table1 AS t1, table2 AS t2

When I run it, it gives the result threefold.
Say the data in the two tables would be:

| table1 | table2 |
===================
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |

The query gives the result:

| tab1 | tab2 |
===============
| 18 | 45 |

As everyone knows: (1+2+3)*3=18 and (4+5+6)*3=45

Both tables have identical settings for the fields fld1 and fld2, they just have a different name.

Can anyone tell me what's going wrong?
Thanks for your time!
 
Nothing's going wrong, except your query logic!

The join table1 AS t1, table2 AS t2 gives the following result set:

t1.hits t2.hits
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6

which gives totals of 18 and 45.

You need two separate queries, for table1 and table2.



-----
ALTER world DROP injustice, ADD peace;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top