我的表格结构如下
if (typeof importClass != "function")
{
load("nashorn:mozilla_compat.js");
}
我需要输出为
name type total
name1 type1 10
name1 type1 10
name1 type2 20
name1 type3 30
name2 type1 30
name2 type2 40
name2 type3 50
name2 type4 25
如何使用php和mysql实现此目的。谁能帮我解决这个问题
注意:这里的type1,type2 ..是动态名称,不能为 硬编码
答案 0 :(得分:0)
Try:
select name, if(type='type1', sum(type),0) as type1,
if(type='type2', sum(type),0) as type2 .................... from table_name group by name
答案 1 :(得分:0)
您可以在下面尝试-
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(type = ''',
type,
''', total, NULL)) AS ',
type
)
) INTO @sql
FROM order_products_sizes;
SET @sql = CONCAT('SELECT name, ', @sql, '
FROM tablename group by name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;