MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 21st, 2012, 08:14 PM
mmmosias mmmosias is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 18 mmmosias User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 55 sec
Reputation Power: 0
Variable table name in SELECT statement

What is the way to write a SELECT statement to retrieve data from several tables with the same name pattern. I'd like to select data from these tables: table_2012, table_2013, table_2014... I only want the tables that have the prefix (e.g. table_) and exactly the four characters for the year. I don't want tables that have a different pattern than the year. Is this doable in MySQL? What is the

SELECT * FROM TABLE LIKE table_% WHERE ....

Reply With Quote
  #2  
Old December 21st, 2012, 08:49 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,714 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 5 m 48 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Not like that no.

The real problem is that you have a bunch of tables all for specific years. You really should be storing everything in one table, and including an extra column to indicate the year (if there isn't already something you can use).

You may be able to create VIEWs for each of the tables you're deprecating.
Code:
CREATE VIEW `table_2012` AS SELECT * FROM `table` WHERE `yearValue` = 2012;

If that doesn't work I'd even consider using triggers (per-year tables have INSERT/UPDATE triggers to copy the action in the main table, main table has basically the same but going into the per-year tables) just so that you can collect all the data in one master location.


But to answer the question you can do a UNION between all of them. (Still have to write the queries though.)
Code:
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...

With a "UNION" alone MySQL will try to remove duplicates for you. If you want them, or know that there won't be any, "UNION ALL" will instruct it not to do so.

Last edited by requinix : December 21st, 2012 at 08:53 PM.

Reply With Quote
  #3  
Old December 22nd, 2012, 10:23 AM
mmmosias mmmosias is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 18 mmmosias User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 55 sec
Reputation Power: 0
These are all good suggestions. But I was wondering whether there was a way to define the SELECT statement to look for tables with a certain pattern in their name, similar to the LIKE feature for columns.

Thanks.


Quote:
Originally Posted by requinix
Not like that no.

The real problem is that you have a bunch of tables all for specific years. You really should be storing everything in one table, and including an extra column to indicate the year (if there isn't already something you can use).

You may be able to create VIEWs for each of the tables you're deprecating.
Code:
CREATE VIEW `table_2012` AS SELECT * FROM `table` WHERE `yearValue` = 2012;

If that doesn't work I'd even consider using triggers (per-year tables have INSERT/UPDATE triggers to copy the action in the main table, main table has basically the same but going into the per-year tables) just so that you can collect all the data in one master location.


But to answer the question you can do a UNION between all of them. (Still have to write the queries though.)
Code:
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...

With a "UNION" alone MySQL will try to remove duplicates for you. If you want them, or know that there won't be any, "UNION ALL" will instruct it not to do so.

Reply With Quote
  #4  
Old December 22nd, 2012, 10:59 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 33 m 50 sec
Reputation Power: 4140
Quote:
Originally Posted by mmmosias
But I was wondering whether there was a way to define the SELECT statement to look for tables with a certain pattern in their name, similar to the LIKE feature for columns.
there isn't

you could do it with an application language like php, by reading the information_schema tables to build the query, but that's two separate steps
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Variable table name in SELECT statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap