I would like to ask if it is at all possible to let MySQL interpret the result of a sub-query as a table column to select from and not just a string?
What I have is are some tables in which some columns are dynamically created, and a table that stores these custom columns that are created.
In this particular implementation, I am using this to store custom fields for custom articles for a content management system we are developing.
Here is a rough sketch of the schema.
custom_column
-----------------------------------------------------------------
id field_name field_type field_display table_name
-----------------------------------------------------------------
1 news_author string Author news
2 news_body text Body news
3 news_heading title Heading news
4 name title Name testimonials
5 message text Message testimonials
Considering that all articles (news, testimonials, etc.) has a corresponding custom field of the field_type = title…
I’d like to just specify a particular table/article and get just the article titles of all records, by doing something like:
SELECT (SELECT field_name FROM custom_columns WHERE field_type = "title" AND table_name = "testimonials") AS article_title FROM testimonials
And get a result along the lines of:
id article_title
-----------------------
1 John Doe
2 Mary Jane
3 Justin Case
And not this, which is what MySQL gives me via the above query:
id article_title
-----------------------
1 name
2 name
3 name
I can see that MySQL is interpreting name – the result of the sub-query – as a string, and not a column name to select from.
Of course, I can do something like:
SELECT *, (SELECT field_name FROM custom_columns WHERE field_type = "title" AND table_name = "testimonials") AS article_title FROM testimonials
And manipulate the results to get what I want within the program, like:
$article_title = $row->{$row->article_title};
//I use PHP, and most times use mysql_fetch_object to fetch rows from a MySQL resource.
But I wish to avoid querying all the the useless columns which I don’t need and hopefully save resources. After all, I just need one column out of a variable amount of them, which in theory can be quite many.
I’ve scoured everywhere for a MySQL function that I may be missing for this to work, but never found one yet. I hope someone can point me to the right direction, or better yet, offer a better solution if one exist.
Thanks!
EDIT: To those that might be unfamiliar with the PHP syntax above, here is an equivalent scenario, but using the very common array syntax in place of PHP’s syntax for objects:
$row[$row['article_title']];
The general rule of thumb is that any time you need to dynamically look up a column in SQL (PHP is different), you should probably rethink your schema. If you ask a question on how to do that, I’m sure that you will find plenty of SO’ers willing to help.
The only dynamic MySQL I’ve ever found was demonstrated in this question, everything else is a variant on that. Your
$article_title = $row->{$row->article_title};is realistically the best solution unless you can know the possible column names ahead of time.