Jump to content

How do I SELECT from a Table, whose name is contained in a column?


leash123

Recommended Posts

Okay, weird issue...

 

I have a 6 tables, all with the same structure, but I've separated them to reduce computation time (each table corresponds to a category, and so the 6 tables are independent of one another).

 

I'm doing a SELECT statement from another table, which has one column which contains the name of one of the 6 tables. Because I'm trying to do this all in one a single SQL call, what I'm trying to do (but this won't work) is:

 

SELECT id, (SELECT name FROM a.table_name WHERE id=a.id) as name FROM linkage_table a;

 

Where linkage_table contains a bunch of columns, including the column table_name, which is the name of the table that I want to do the inner-select statement...

 

MySQL returns the error that a.table_name doesn't exist.

 

Any help?

 

Sorry if this is tough to understand, :) Let me know if I didn't explain it well, and I'll try again.

Link to comment
Share on other sites

Okay, weird issue...

 

I have a 6 tables, all with the same structure, but I've separated them to reduce computation time (each table corresponds to a category, and so the 6 tables are independent of one another).

 

I'm doing a SELECT statement from another table, which has one column which contains the name of one of the 6 tables. Because I'm trying to do this all in one a single SQL call, what I'm trying to do (but this won't work) is:

 

SELECT id, (SELECT name FROM a.table_name WHERE id=a.id) as name FROM linkage_table a;

 

Where linkage_table contains a bunch of columns, including the column table_name, which is the name of the table that I want to do the inner-select statement...

 

MySQL returns the error that a.table_name doesn't exist.

 

Any help?

 

Sorry if this is tough to understand, :) Let me know if I didn't explain it well, and I'll try again.

 

You can't do choose the table name dynamically... do it in 2 sql statements, or decide programmtically.

Link to comment
Share on other sites

I'm not sure if you meant this when you said you would use a CASE, but you could LEFT JOIN each of the possible tables and return the one where `name` IS NOT NULL.  Could be performance issues with that though.

 

Yeah... bad idea... since you don't have to "guess" the table name, do it in php.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.