Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:24:10+00:00 2026-05-25T00:24:10+00:00

I would like to ask if it is at all possible to let MySQL

  • 0

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']];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:24:11+00:00Added an answer on May 25, 2026 at 12:24 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask if there is a possible way to select all
I would like to ask if it is possible to copy/move all the objects
all. I would like to ask if it is possible to have multiple forms
I would like to ask you if it's possible (and how), to copy all
I would like ask if there's a way to download an android layout from
Hi I would like to ask if its possible get array with clear data
I would like to ask in what situations could this even be possible? I've
First of all, I would like to ask, CAN YOU integrate Bing Maps within
I would like to ask about best practices or the simplest way possible of
I would like to ask if it is possible to add an element into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.