leash123 Posted September 23, 2007 Share Posted September 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/ Share on other sites More sharing options...
leash123 Posted September 23, 2007 Author Share Posted September 23, 2007 So I've decided to do a CASE select, and compare it as a string. I'd rather not do it this way, but I can't figure out any alternative. Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/#findComment-353192 Share on other sites More sharing options...
effigy Posted September 23, 2007 Share Posted September 23, 2007 It sounds like you need to modify your database design. You can also try using PHP to run multiple queries, or creating a stored procedure. Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/#findComment-353231 Share on other sites More sharing options...
leash123 Posted September 23, 2007 Author Share Posted September 23, 2007 I think a stored procedure would be my other option. I want to avoid using PHP to do any lazy loading, 'cause computation time would skyrocket (there's a lot that'll be going on, so if I can keep it to one statement that'd be best) Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/#findComment-353242 Share on other sites More sharing options...
fenway Posted September 24, 2007 Share Posted September 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/#findComment-354114 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/#findComment-354124 Share on other sites More sharing options...
fenway Posted September 24, 2007 Share Posted September 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70308-how-do-i-select-from-a-table-whose-name-is-contained-in-a-column/#findComment-354265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.