join - MySQL query to return custom structure with values as field type and titles -


sorry bizarre title, isn't easy summarize want achieve. goal have general table structure store "any" kind of data, , make it... describe itself, custom table output of query.

i have table structure:

id (int) iditem (int) fieldtitle (varchar) field (varchar) value_label (varchar) value_text (text) value_bool (tinyint) value_int (int) value_float (float) 

an example of data inside table:

id  iditem  fieldtitle  field       value_label     value_text              value_bool  value_int   value_float -------------------------------------------------------------------------------------------------------------- 1   15      title       value_label product 1  null                    null        null        null 2   15      description value_text  null            product...    null        null        null 3   15      visible     value_bool  null            null                    1           null        null 4   15      price       value_float null            null                    null        null        19.9 5   16      title       value_label product 2  null                    null        null        null 6   16      description value_text  null            hidden product. null        null        null 7   16      visible     value_bool  null            null                    0           null        null 8   16      price       value_float null            null                    null        null        30 

i want end having this:

iditem (int) title  (varchar) description (text) visible (tinyint) price (float) 

example of output data:

iditem  title           description             visible price 15      product 1  product...    1       19.9 16      product 2  hidden product. 0       30 

although don't agree kind of schemas (see comment below question) can give try:

select iditem,   max(if(fieldtitle = 'title', value_label, null)) title,   max(if(fieldtitle = 'description', value_text, null)) description,   max(if(fieldtitle = 'visible', value_bool, null)) visible,   max(if(fieldtitle = 'price', value_float, null)) price t group iditem 

this results in:

+--------+----------------+-------------------------+---------+-------+ | iditem |     title      |       description       | visible | price | +--------+----------------+-------------------------+---------+-------+ |     15 | my_product_one | very_good_product...    |       1 | 19.9  | |     16 | my_product_two | just_an_hidden_product. |       0 | 30    | +--------+----------------+-------------------------+---------+-------+ 

adding more fields straight forward. let me know if have ay issues it.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -