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 > Create generic query for table with generic name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-27-11, 05:00
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Question Create generic query for table with generic name

Hi guys,

I have about 20 tables with a generic name like :
table_1
table_2
table_3
table_4
table_5
...
table_20

I need to execute the same query on each table. As I'm a bit tired to copy paste the same thing 20 times in a text file .. Can I do something like a for loop or whatever that does the same and would execute my query

Code:
i=1
for i [1, 20] {
go to table_i ... do the query
i=+1
}
I know it might be confusing .. but it's hard to explain sorry
Reply With Quote
  #2 (permalink)  
Old 05-27-11, 05:21
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
frankly that stinks of bad design
20+ tables with a similar structure
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 05-27-11, 05:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by madkitty View Post
As I'm a bit tired to copy paste the same thing 20 times in a text file ..
oh, you should have thought of this when you designed 20 identical tables

of course, redesigning them is probably a lot more work than copy/paste, isn't it...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-27-11, 10:14
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Yes it is possible but as the others say it does seem that your design is not ideal. Have a look at stored procedures and in particular to PREPARE, EXECUTE and DEALLOCATE PREPARE which allows you do dynamically create statements.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 05-28-11, 23:14
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
yeah i know the design sucks but I have no other option. Our data are over 6GB per table, I couldn't figure out any other design so far ..

I'll read more on the prepare/execute/deallocate prepare statements.
Thanks for your help~
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