mysql - How to display one table values as table row and display values for this from another table -
i'm trying combine , display 2 table bellow, first table structure is,
my second table is,
and expected result is,
i tried could't result
select t2.field_value t2.name content_fields t1 join content_fields_type_richtext_data t2 on t1.id = t2.field_id; is there mistake in table structure. how result. please one
mysql not have pivot function can convert row data columns using aggregate function case expression:
select t2.object_id, max(case when t1.frontend_name = 'bride photo' t2.field_value end) `bride photo`, max(case when t1.frontend_name = 'bridegroom photo' t2.field_value end) `bridegroom photo`, max(case when t1.frontend_name = 'bride description' t2.field_value end) `bride description`, max(case when t1.frontend_name = 'groom description' t2.field_value end) `groom description`, max(case when t1.frontend_name = 'wishes' t2.field_value end) `wishes` content_fields t1 inner join content_fields_type_richtext_data t2 on t1.id = t2.field_id group t2.object_id; if have unknown or dynamic number of values, want use prepared statement result:
set @sql = null; select group_concat(distinct concat( 'max(case when t1.frontend_name = ''', frontend_name, ''' t2.field_value end) `', frontend_name, '`' ) ) @sql content_fields; set @sql = concat('select t2.object_id, ', @sql, ' content_fields t1 inner join content_fields_type_richtext_data t2 on t1.id = t2.field_id group t2.object_id'); prepare stmt @sql; execute stmt; deallocate prepare stmt;
Comments
Post a Comment