José Carlos
José Carlos

Reputation: 1015

MySQL - is it possible to use a column as a tablename?

Suppose that we have, in mysql, a table "mytable"

with the columns: id, title, type.

Is it possible to use its column as a table name in the same query?

For example:

SELECT m.id, m.title FROM mytable m INNER JOIN m.type WHERE m.id=2

Where "type" will give me the name of the table to do the inner join.

Upvotes: 0

Views: 99

Answers (2)

Kevin Burton
Kevin Burton

Reputation: 11936

Why not put all of your type data in a single table ? and then partition the table by type.

Upvotes: 0

MatBailie
MatBailie

Reputation: 86706

No, Sorry :(

The closest you can get (afaik) is to cursor through your main table and write dynamic html for the join for each row. VERY slow.

Or, find a new design pattern - Is there scope for you to post a question about what you are trying to achieve and how people may go about that?

Upvotes: 3

Related Questions