mysql query to dynamically convert row data to columns

前端 未结 4 1437
广开言路
广开言路 2020-12-10 08:54

I am working on a pivot table query. The schema is as follows

Sno, Name, District

The same name may appear in many districts eg take the sample data for exam

4条回答
  •  悲哀的现实
    2020-12-10 09:01

    a) "For each " is not supported in MySQL stored procedures. b) Stored procedures cannot execute prepared statements from concatenated strings using so called dynamic SQL statements, nor can it return results with more than One distinct row. c) Stored functions cannot execute dynamic SQL at all.

    It is a nightmare to keep track of once you got a good idea and everyone seems to debunk it before they think "Why would anyone wanna..."

    I hope you find your solution, I am still searching for mine. The closes I got was

    (excuse the pseudo code)

    -> to stored procedure, build function that...

    1) create temp table 2) load data to temp table from columns using your if statements 3) load the temp table out to INOUT or OUT parameters in a stored procedure as you would a table call... IF you can get it to return more than one row

    Also another tip... Store your districts as a table conventional style, load this and iterate by looping through the districts marked active to dynamically concatenate out a querystring that could be plain text for all the system cares

    Then use;

    prepare stmName from @yourqyerstring; execute stmName; deallocate prepare stmName;

    (find much more on the stored procedures part of the mysql forum too)

    to run a different set of districts every time, without having to re-design your original proc

    Maybe it's easier in numerical form. I work on plain text content in my tables and have nothing to sum, count or add up

提交回复
热议问题