Forum Moderators: open

Message Too Old, No Replies

mysql variable table names and column names

         

noyearzero

12:25 am on Mar 3, 2009 (gmt 0)

10+ Year Member



the mysql documentation says its possible to use subqueries as the name a table to select from. Although I can't get that to work, what i'm really hoping is possible is to use a value gathered in an outer query as the name of a table in a subquery.

for example i have a table called positions:

ID, FOR_TABLE, FOR_ID
1, articles, 10
2, authors, 20

my articles table:
ID, CONTENT, STATUS
10, BlahBlah, 1

my authors table:
ID, CONTENT, STATUS
20, BlahBlah, 0

basically i'd like to return the status for each row of table 'positions'.

so would i be able to do this...

SELECT * FROM positions LEFT JOIN positions.FOR_TABLE AS t ON positions.FOR_ID = t.ID

?

My problem is actually more complex than this...so i'm taking this step by step. would this work, or is there another syntax i'd need?

physics

4:33 pm on Mar 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi noyearzero. I know this doesn't answer your question but are you sure you wouldn't be better off doing this in your PHP (or whatever language you're using) code?

noyearzero

2:19 am on Mar 4, 2009 (gmt 0)

10+ Year Member



it would be possible in PHP, but it is going to run for a long list of items so it would be optimal to use a subquery.