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 > How do I generate dynamic table names in a SELECT statement?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-06, 11:36
boftx boftx is offline
Registered User
 
Join Date: Dec 2006
Posts: 6
How do I generate dynamic table names in a SELECT statement?

I need to generate the table name for the FROM clause on-the-fly. I can do it by building a string and then using that, but I really need to do it in a JOIN clause if possible.

NOTE! I did NOT design this structure, it is an inherited project.

Here is a basic example of what I need:

Code:
select foo from CONCAT('regtable_',(select regtable from event where id = ?));
The simplified schema looks like this:

Code:
Table event:
  id - int,
  regtable - int

Table regtable_1:
  regid - int,
  eventID - int,
  foo - varchar

Table regtable_2:
  regid - int,
  eventID - int,
  foo - varchar
Currently I have to select the regtable field from the event table, build a string from that and then use that string. I would like to do it all in a single SQL statement if possible. (And I am not sure at all that this is possible).

Thanks in advance!

Jim
Reply With Quote
  #2 (permalink)  
Old 12-02-06, 11:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i don't think it's possible
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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