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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:06:19+00:00 2026-06-04T08:06:19+00:00

I have three MYSQL tables, simplified, with following columns: ModuleEntries (MDE_ID) ModuleFields (MDF_ID, MDF_Label)

  • 0

I have three MYSQL tables, simplified, with following columns:

ModuleEntries (MDE_ID)
ModuleFields (MDF_ID, MDF_Label)
ModuleEntryFieldValues (MFV_ID, MFV_MDE_ID, MFV_MDF_ID, MFV_Value)

As you can see from the simplified column list, ModuleEntryFieldValues is the table which states that “for an entry, this field has this value”.

Now, I would like to retrieve this information in one “flat” recordset. I have managed to get things working, but not entirely to what i want.

With the help of a different SO article, I managed to get the dynamic, variable amount of columns to work, by using a cursor.

  declare finish int default 0;
  declare fldid int;
  declare str varchar(10000) default 'SELECT *,';
  declare curs cursor for select MDF_ID from ModuleFields group by MDF_ID;
  declare continue handler for not found set finish = 1;
  open curs;
  my_loop:loop
    fetch curs into fldid;
    if finish = 1 then
      leave my_loop;
    end if;
    set str = concat(str, 'max(case when MFV_MDF_ID = ',fldid,' then MFV_Value else null end) as field_',fldid,',');
  end loop;
  close curs;
  set str = substr(str,1,char_length(str)-1);
  set @str = concat(str, ' from ModuleEntries LEFT join ModuleEntryFieldValues ON MDE_ID = MDF_MDE_ID GROUP BY MDE_ID');
  prepare stmt from @str;
  execute stmt;
  deallocate prepare stmt;

What this code doesn’t do, is allow me to put the column values of MDF_Label as the actual column headers of the rows. Now, the above code gives me “field_1, field_2, …”

Is it possible to join these three tables, and have the MDF_Label as column header for the rows that are now columns in the joined table ?

I want this…

ModuleEntries  |   ModuleFields         |   ModuleEntryFieldValues
-------------  |   ------------------   |   -----------------------------------
MDE_ID         |   MDF_ID - MDF_Label   |   MFV_MDE_ID - MFV_MDF_ID - MFV_Value
1              |   1      - Height      |   1          - 1          - 120cms
               |   2      - Width       |   1          - 2          - 30cms
               |   3      - Weight      |   

into this

Recordset
---------
MDE_ID - Height - Width - Weight
1      - 120cms - 30cms - null

I hope my question was clear enough. If not please comment and I will give more information where needed, if I can.

  • 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-06-04T08:06:21+00:00Added an answer on June 4, 2026 at 8:06 am

    I’d just use GROUP_CONCAT() instead of cursors to generate the prepared statement:

    SELECT CONCAT(
     ' SELECT MDE_ID,'
    
    ,  GROUP_CONCAT(
         't', MDF_ID, '.MFV_Value AS `', REPLACE(MDF_Label, '`', '``'), '`'
       )
    
    ,' FROM ModuleEntries '
    
    , GROUP_CONCAT(
        'LEFT JOIN ModuleEntryFieldValues AS t', MDF_ID, '
           ON t', MDF_ID, '.MFV_MDE_ID = ModuleEntries.MDE_ID
          AND t', MDF_ID, '.MFV_MDF_ID = ', MDF_ID
      SEPARATOR ' ')
    
    ) INTO @qry FROM ModuleFields;
    

    Save for whitespace editing to make it more readable, with your sample data @qry would then contain:

    SELECT MDE_ID,
           t1.MFV_Value AS `Height`,
           t2.MFV_Value AS `Width`,
           t3.MFV_Value AS `Weight`
    FROM   ModuleEntries
      LEFT JOIN ModuleEntryFieldValues AS t1
             ON t1.MFV_MDE_ID = ModuleEntries.MDE_ID AND t1.MFV_MDF_ID = 1
      LEFT JOIN ModuleEntryFieldValues AS t2
             ON t2.MFV_MDE_ID = ModuleEntries.MDE_ID AND t2.MFV_MDF_ID = 2
      LEFT JOIN ModuleEntryFieldValues AS t3
             ON t3.MFV_MDE_ID = ModuleEntries.MDE_ID AND t3.MFV_MDF_ID = 3
    

    One can then prepare and execute that statement:

    PREPARE stmt FROM @qry;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    SET @qry = NULL;
    

    Which yields the following results:

    MDE_ID    Height    Width    Weight
         1    120cms    30cms    (null)
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have multiple MySQL tables containing varying numbers of columns. After joining three of
I have three mysql tables items =========== id title items_in_categories ============================ id item_id category_id
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I have a MySQL query that: gets data from three tables linked by unique
I have a mysql table with three columns: username, location, timestamp. This is basically
I have multiple columns in a mySQL table. Three of the columns are named
I have three mysql tables, first holds information about articles (Article ID, author ID,
I have three tables in a MySQL database used in a music library application:
I have a MySQL database with two tables (simplified for this question): movies table

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.