MySQL数据库行转列
一、数据源如下所示
二、对应SQL语句如下所示
-- 行转列
SELECT t1.`产品名称`,
SUM(CASE t1.`日期` WHEN '2019-11-11' THEN t1.`数量` ELSE 0 END) AS '2019-11-11',
SUM(CASE t1.`日期` WHEN '2019-11-12' THEN t1.`数量` ELSE 0 END) AS '2019-11-12',
SUM(CASE t1.`日期` WHEN '2019-11-13' THEN t1.`数量` ELSE 0 END) AS '2019-11-13',
SUM(CASE t1.`日期` WHEN '2019-11-14' THEN t1.`数量` ELSE 0 END) AS '2019-11-14',
SUM(CASE t1.`日期` WHEN '2019-11-15' THEN t1.`数量` ELSE 0 END) AS '2019-11-15',
SUM(CASE t1.`日期` WHEN '2019-11-16' THEN t1.`数量` ELSE 0 END) AS '2019-11-16',
SUM(CASE t1.`日期` WHEN '2019-11-17' THEN t1.`数量` ELSE 0 END) AS '2019-11-17'
FROM sheet2 t1 GROUP BY t1.`产品名称` LIMIT 0, 1000;
三、最终结果如下所示