MySQL:如何在SELECT cluase中使用Multi-SELECT?

时间:2018-05-14 16:47:05

标签: mysql select subquery

我有一个表格,名称​​ aaa

year   month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1993   4     2.4

我想要一个这样的结果:

year m1   m2   m3   m4
1991 1.1  1.2  1.3  1.4
1992 2.1  2.2  2.3  2.4 

作为一名新手,我无法在数小时内找到出路并进行体验。我尝试了但失败了:

SELECT year,
(SELECT amount FROM aaa WHERE month=1) AS m1,
(SELECT amount FROM aaa WHERE month=2) AS m2,
(SELECT amount FROM aaa WHERE month=3) AS m3,
(SELECT amount FROM aaa WHERE month=4) AS m4
FROM aaa GROUP BY year;  

有人能告诉我吗?或者让我知道在哪里可以找到答案? Thx提前。

或者这个:

SELECT year,
(SELECT amount FROM aaa m WHERE month=1 AND m.year=aaa.year) AS m1,
(SELECT amount FROM aaa m WHERE month=2 AND m.year=aaa.year) AS m2,
(SELECT amount FROM aaa m WHERE month=3 AND m.year=aaa.year) AS m3,
(SELECT amount FROM aaa m WHERE month=4 AND m.year=aaa.year) AS m4
FROM aaa GROUP BY year;

0 个答案:

没有答案