dbTalk Databases Forums  

Help with Query on multiple tables

comp.databases comp.databases


Discuss Help with Query on multiple tables in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
globring@gmail.com
 
Posts: n/a

Default Help with Query on multiple tables - 08-18-2006 , 09:21 AM






This particular query I am trying to do is using MySQL, but its
probably the same with any relational database:

Scenario:

I have 20 tables, all structured exactly the same. Each one only has
two columns, I'll call "week" which has a numerical value (1, 2, 3,
etc.) and "team" which has a sports team name (bears, tigers, tunas,
etc.).

I'm trying to write a query that will query all 20 tables, for just a
particular week #, where the team name is a particular team and give me
a count.

For example, for week #1, I want to know throughout all my tables, who
picked the tigers to win. So on all the tables where week=1, how many
values of "tigers" are there throughout all 20 tables. Obviously, the
max value obtained would be 20, the min would be 0.

It seems simple to me, but dangit I just can't get it to work.

Any help is greatly appreciated!


Reply With Quote
  #2  
Old   
strawberry
 
Posts: n/a

Default Re: Help with Query on multiple tables - 08-18-2006 , 10:46 AM







globring (AT) gmail (DOT) com wrote:
Quote:
This particular query I am trying to do is using MySQL, but its
probably the same with any relational database:

Scenario:

I have 20 tables, all structured exactly the same. Each one only has
two columns, I'll call "week" which has a numerical value (1, 2, 3,
etc.) and "team" which has a sports team name (bears, tigers, tunas,
etc.).

I'm trying to write a query that will query all 20 tables, for just a
particular week #, where the team name is a particular team and give me
a count.

For example, for week #1, I want to know throughout all my tables, who
picked the tigers to win. So on all the tables where week=1, how many
values of "tigers" are there throughout all 20 tables. Obviously, the
max value obtained would be 20, the min would be 0.

It seems simple to me, but dangit I just can't get it to work.

Any help is greatly appreciated!
Can I ask a really dumb question: Why are there 20 tables, and not just
1?



Reply With Quote
  #3  
Old   
Bill Karwin
 
Posts: n/a

Default Re: Help with Query on multiple tables - 08-18-2006 , 11:27 AM



globring (AT) gmail (DOT) com wrote:
Quote:
I'm trying to write a query that will query all 20 tables, for just a
particular week #, where the team name is a particular team and give me
a count.
SELECT team, COUNT(week)
FROM (
SELECT team, week FROM table01
UNION ALL SELECT team, week FROM table02
...
UNION ALL SELECT team, week FROM table19
UNION ALL SELECT team, week FROM table20
) AS composite_table
WHERE week = 1
GROUP BY team;

This is assuming your RDBMS supports subqueries as derived tables.

Though I agree with strawberry that it is unusual and probably not the
best design to have to do this. All your 20 tables should be combined
into one.

Regards,
Bill K.


Reply With Quote
Reply




Thread Tools
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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.