Link to home
Start Free TrialLog in
Avatar of fdcol63
fdcol63Flag for United States of America

asked on

Create temp table or subquery to use results of stored procedure in MySQL that pivots view results into dynamic columns

I have a MySQL stored procedure that pivots the data from a view with dynamic columns.  This is working fine, but just temporarily returns the output on the screen in phpMyAdmin and I can't then use the data for anything, such as to display the query results.

I suppose I need to output the results to a temporary table or subquery so that I can use the results.  Any advice on how I can do this?

The code that works that returns the pivot data is:

===================================
SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(IF(p.FieldName = ''',
      FieldName,
      ''', p.FieldValue, NULL)) AS ',
      FieldName
    )
  ) INTO @sql
FROM Store_Items;

SET @sql = CONCAT('SELECT p.SubmissionId
                    , p.FormId                  
                    , p.DateSubmitted
                    , p.UserId
                    , p.confirmed, ', @sql, '
                   FROM Store_Items p
                   GROUP BY p.SubmissionId');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
===================================

Would a temp table be what I need, or possibly some other subquery before I DEALLOCATE PREPARE?

What code would I need to add/change to do this?

Any help would be greatly appreciated!
Avatar of skullnobrains
skullnobrains

You can view anx export any query results that you can see in phpmyadmin

You can rewrite the procedure as a single query, using your first query as a subquery fot the second

Actually ATsql is a tmp table
Avatar of fdcol63

ASKER

skullnobrains,

Thanks for your suggestion.

I found a solution by creating a scheduled event to run the stored procedure.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of fdcol63
fdcol63
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not sure i was actually helpful...

A disk based view would seemingly update itself automagically. The results would be always up to date but the insert performance would suffer somehow

Given the query, using triggers should be faster but i believe the tmp table is MUCH easier to setup

Anyway if your solution fits the bill...