从查询mysql转置结果

时间:2016-06-14 12:58:48

标签: mysql

我认为这是一个愚蠢的问题。是否可以在mysql上转换查询结果?

我有这样的查询:

SELECT SUM(a.BALANCE) AS STORAGE,
 (SELECT SUM(LIFT_ON) FROM tb_fin_repo b WHERE LPAD(MONTH( b.DATE_OUT), 2, 0) = 05) AS REPO,
 (SELECT SUM(CLEANING) FROM tb_fin_cleaning c WHERE LPAD(MONTH(c.OUT_DATE), 2, 0) = 05) AS CLEANING,
 (SELECT SUM(ONE_BAR) FROM tb_fin_one_bar d WHERE LPAD(MONTH(d.OUT_DATE), 2, 0) = 05) AS ONE_BAR,
 (SELECT SUM(YEARS2) FROM tb_fin_test_periodic e WHERE LPAD(MONTH(e.OUT_DATE), 2, 0) = 05) AS YEARS2,
 (SELECT SUM(YEARS5) FROM tb_fin_test_periodic f WHERE LPAD(MONTH(f.OUT_DATE), 2, 0) = 05) AS YEARS5,
 (SELECT SUM(REPAIR) FROM tb_fin_repair g WHERE LPAD(MONTH(g.OUT_DATE),2,0)) AS REPAIR

 FROM tb_fin_storage a
 WHERE LPAD(MONTH( a.OUT_DATE), 2, 0) = 05

结果是:

+---------+--------+----------+---------+--------+--------+---------+
| STORAGE | REPO   | CLEANING | ONE_BAR | YEARS2 | YEARS5 | REPAIR  |
+---------+--------+----------+---------+--------+--------+---------+
| 2210.40 | 108.00 |  9490.00 | 1512.00 | 250.00 | 540.00 | 7651.95 |
+---------+--------+----------+---------+--------+--------+---------+
1 row in set (0.00 sec)

我需要这样:

+-------------+---------+
| DESCRIPTION |  VALUE  |
+-------------+---------+
| STORAGE     | 2210.40 |
| REPO        | 108.00  |
| CLEANING    | 9490.00 |
| ONE_BAR     | 1512.00 |
| YEARS2      | 250.00  |
| YEARS5      | 540.00  |
| REPAIR      | 7651.95 |
+-------------+---------+

是否有可能,因为非常感谢帮助

2 个答案:

答案 0 :(得分:1)

您应该使用UNION ALL

SELECT 'STORAGE' as description, SUM(a.BALANCE) AS STORAGE  FROM tb_fin_storage a
WHERE LPAD(MONTH( a.OUT_DATE), 2, 0) = 0
UNION ALL
SELECT 'REPO',SUM(LIFT_ON) FROM tb_fin_repo b
WHERE LPAD(MONTH( b.DATE_OUT), 2, 0) = 05
UNION ALL
SELECT 'CLEANING' , SUM(CLEANING) FROM tb_fin_cleaning c
WHERE LPAD(MONTH(c.OUT_DATE), 2, 0) = 05) 
UNION ALL
SELECT 'ONE_BAR',SUM(ONE_BAR) FROM tb_fin_one_bar d
WHERE LPAD(MONTH(d.OUT_DATE), 2, 0) = 05) 
UNION ALL
SELECT 'YEARS2' , SUM(YEARS2) FROM tb_fin_test_periodic e
WHERE LPAD(MONTH(e.OUT_DATE), 2, 0) = 05
UNION ALL
SELECT 'YEARS5' , SUM(YEARS5) FROM tb_fin_test_periodic f
WHERE LPAD(MONTH(f.OUT_DATE), 2, 0) = 05
UNION ALL
SELECT 'REPAIR' , SUM(REPAIR) FROM tb_fin_repair g 
WHERE LPAD(MONTH(g.OUT_DATE),2,0)

答案 1 :(得分:1)

这不是我见过的最好的查询,只是为了让你了解你的要求。

您可以使用UNION

SELECT 'STORAGE' as DESCRIPTION,SUM(a.BALANCE) as Value
FROM tb_fin_storage a
WHERE LPAD(MONTH( a.OUT_DATE), 2, 0) = 05
UNION
SELECT 'REPO',SUM(LIFT_ON) 
FROM tb_fin_repo b 
WHERE LPAD(MONTH( b.DATE_OUT), 2, 0) = 05
UNION
SELECT 'CLEANING',SUM(CLEANING) 
FROM tb_fin_cleaning c 
WHERE LPAD(MONTH(c.OUT_DATE), 2, 0) = 05
UNION
SELECT 'ONE_BAR',SUM(ONE_BAR) 
FROM tb_fin_one_bar d 
WHERE LPAD(MONTH(d.OUT_DATE), 2, 0) = 05
UNION
SELECT 'YEARS2',SUM(YEARS2) 
FROM tb_fin_test_periodic e 
WHERE LPAD(MONTH(e.OUT_DATE), 2, 0) = 05
UNION
SELECT 'YEARS5',SUM(YEARS5) 
FROM tb_fin_test_periodic f 
WHERE LPAD(MONTH(f.OUT_DATE), 2, 0) = 05
UNION
SELECT 'REPAIR', SUM(REPAIR) 
FROM tb_fin_repair g 
WHERE LPAD(MONTH(g.OUT_DATE),2,0)

您是否忘记= 05上的tb_fin_repair WHERE