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 6624643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:40:42+00:00 2026-05-25T21:40:42+00:00

everyone i would like to perform a query on MySQL with dynamic rows. Imagine

  • 0

everyone i would like to perform a query on MySQL with dynamic rows.

Imagine this:

table phones_categories

+----+------------+
| id | name       |
+----+------------+
|  1 | Home Phone |
|  2 | Cell Phone | 
|  3 | Fax        | 
+----+------------+

table phones

+----+-----------+-------------------+--------------+
| id | entity_id | phone_category_id | phone_number |
+----+-----------+-------------------+--------------+
|  1 |         1 |                 1 | X19 XXX 2XX  |
|  2 |         1 |                 3 | X19 XXX 2XX  |
|  3 |         2 |                 1 | X18 XXX 4XX  |
|  4 |         2 |                 3 | X18 XXX 4XX  |
+----+-----------+-------------------+--------------+

i would like to have the following output:

+-----------+--------------+--------------+-------------+
| entity_id | Home Phone   | Cell Phone   | Fax         |
+-----------+--------------+--------------+-------------+
|         1 |  X19 XXX 2XX |              | X19 XXX 2XX |             
|         2 |  X18 XXX 4XX |              | X18 XXX 4XX |
+-----------+--------------+--------------+-------------+

Ok i need some “dynamic” becacuse on the future the table phone_categories can grow.

  • 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-25T21:40:43+00:00Added an answer on May 25, 2026 at 9:40 pm

    This is called a “pivot table” or “crosstab query”. MySQL alone cannot do this dynamically. You always need to know the column names ahead of time, so if you are using a programming/scripting language for your output you can use it to dynamically build up the SQL statement with a for loop after you’ve determined the categories.

    But the query will look like:

    SELECT
      phones.entity_id,
      CASE WHEN phones.phone_category_id = 1 THEN phones.phone_number ELSE NULL END AS `Home Phone`,
      CASE WHEN phones.phone_category_id = 2 THEN phones.phone_number ELSE NULL END AS `Cell Phone`,
      CASE WHEN phones.phone_category_id = 3 THEN phones.phone_number ELSE NULL END AS `Fax`
    FROM phones
    

    You haven’t identified any programming language, so here’s some pseudocode to generate the query:

    categories = "SELECT id, name FROM phone_categories;"
    foreach categories
      sql_columns = sql_columns + " CASE WHEN phones.phone_category_id = " + categories.id + " THEN phones.phone_number ELSE NULL END AS  `categories.name`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like some opinion, example and code on how everyone dose this.. So
Because apparently everyone hates sub selects, I would like to do this using joins.
First, I would to thank everyone for all the help they provide via this
Hello Everyone I would like to open a rich:popupPanel inside a conditional expression, actually
I would like to know your opinions on how to handle this. I need
Hey everyone. I would like to create a joomla menu but have it display
I would like users do to it themselves, but I don't want everyone to
I have been looking around and would like to know what everyone uses to
I would like to export an HTML table to a MS Excel document (and
I would like to search my table having a column of first names and

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.