MySQL: select on a table with tablename from a recordset
I would like to do this in one step so I would like to know how can I:
table a (documents
) has: pk, document_id, template_id
table b (templates
) has: pk, template_id, template_table_name
table c (template_table_name_1
) has: pk, document_id, document_specific_columns
so... I would like to query table a, get document_id and template_id and then using table b i would like to get document_specific_columns from table c. PS: in table a can be more records with same document开发者_运维百科_id but different template_id I guess the intentions are pretty clear, the only thing I don't know how to do is to reference FROM clause of the subquery that returns te template_table_name1 to a table
The easiest way to do this is to build the SQL query on whatever your client is.
So, the first SQL query would be
select t.template_table_name
from templates t,
documents d
where d.template_id = t.template_id
and d.document_id = @param_document_id
You would then - on your client - build a second SQL query to select * from whatever template_table_name is.
If you want to do it all in one run to the database, you'd have to do it in a stored procedure. MySQL doesn't support dynamic SQL, but you can cheat by creating prepared statements.
http://forums.mysql.com/read.php?60,3127,6260#msg-6260
精彩评论