如何选择包含所选表的新列?

时间:2018-08-13 02:18:40

标签: mysql sql innodb

如何选择包含所选表的新列?我尝试执行此操作,但返回错误消息(#1242-子查询返回多于1行)

SELECT 
(SELECT SUM(A+AA+AB+AC+AD+AE+AF+AG+AH+AI+AJ+AK+AL+AM+AN+AO+
        B+C+CL+D+E+F+G+H+I+J+K+L+LK+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z) 
        AS SELESAI
        FROM statistik WHERE stastus = '1' GROUP BY kategori ),

SUM(A+AA+AB+AC+AD+AE+AF+AG+AH+AI+AJ+AK+AL+AM+AN+AO+
    B+C+CL+D+E+F+G+H+I+J+K+L+LK+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z) 
    AS Jumlah,kategori, 
SUM(A) AS A, 
SUM(AA) AS AA, 
SUM(AB) AS AB, 
SUM(AC) AS AC, 
SUM(AD) AS AD, 
SUM(AE) AS AE, 
SUM(AF) AS AF, 
SUM(AG) AS AG, 
SUM(AH) AS AH, 
SUM(AI) AS AI, 
SUM(AJ) AS AJ, 
SUM(AK) AS AK, 
SUM(AL) AS AL, 
SUM(AM) AS AM, 
SUM(AN) AS AN, 
SUM(AO) AS AO, 
SUM(B) AS B, 
SUM(C) AS C, 
SUM(CL) AS CL, 
SUM(D) AS D, 
SUM(E) AS E, 
SUM(F) AS F, 
SUM(G) AS G, 
SUM(H) AS H, 
SUM(I) AS I, 
SUM(J) AS J, 
SUM(K) AS K, 
SUM(L) AS L, 
SUM(LK) AS LK,
SUM(M) AS M, 
SUM(N) AS N, 
SUM(O) AS O, 
SUM(P) AS P, 
SUM(Q) AS Q, 
SUM(R) AS R, 
SUM(S) AS S, 
SUM(T) AS T, 
SUM(U) AS U, 
SUM(V) AS V, 
SUM(W) AS W, 
SUM(X) AS X, 
SUM(Y) AS Y, 
SUM(Z) AS Z 
FROM statistik WHERE stastus = '4' GROUP BY kategori ;

这是我的数据库:

-表statistik的表结构

CREATE TABLE `statistik` (
  `id` int(100) NOT NULL,
  `kategori` varchar(100) DEFAULT NULL,
  `stastus` int(100) DEFAULT NULL,
  `tarikhLaporan` date DEFAULT NULL,
  `A` int(2) NOT NULL,
  `AA` int(2) NOT NULL,
  `AB` int(2) NOT NULL,
  `AC` int(2) NOT NULL,
  `AD` int(2) NOT NULL,
  `AE` int(2) NOT NULL,
  `AF` int(2) NOT NULL,
  `AG` int(2) NOT NULL,
  `AH` int(2) NOT NULL,
  `AI` int(2) NOT NULL,
  `AJ` int(2) NOT NULL,
  `AK` int(2) NOT NULL,
  `AL` int(2) NOT NULL,
  `AM` int(2) NOT NULL,
  `AN` int(2) NOT NULL,
  `AO` int(2) NOT NULL,
  `B` int(2) NOT NULL,
  `C` int(2) NOT NULL,
  `CL` int(2) NOT NULL,
  `D` int(2) NOT NULL,
  `E` int(2) NOT NULL,
  `F` int(2) NOT NULL,
  `G` int(2) NOT NULL,
  `H` int(2) NOT NULL,
  `I` int(2) NOT NULL,
  `J` int(2) NOT NULL,
  `K` int(2) NOT NULL,
  `L` int(2) NOT NULL,
  `LK` int(2) NOT NULL,
  `M` int(2) NOT NULL,
  `N` int(2) NOT NULL,
  `O` int(2) NOT NULL,
  `P` int(2) NOT NULL,
  `Q` int(2) NOT NULL,
  `R` int(2) NOT NULL,
  `S` int(2) NOT NULL,
  `T` int(2) NOT NULL,
  `U` int(2) NOT NULL,
  `V` int(2) NOT NULL,
  `W` int(2) NOT NULL,
  `X` int(2) NOT NULL,
  `Y` int(2) NOT NULL,
  `Z` int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-转储表statistik

的数据
INSERT INTO `statistik` (`id`, `kategori`, `stastus`, `tarikhLaporan`, `A`, `AA`, `AB`, `AC`, `AD`, `AE`, `AF`, `AG`, `AH`, `AI`, `AJ`, `AK`, `AL`, `AM`, `AN`, `AO`, `B`, `C`, `CL`, `D`, `E`, `F`, `G`, `H`, `I`, `J`, `K`, `L`, `LK`, `M`, `N`, `O`, `P`, `Q`, `R`, `S`, `T`, `U`, `V`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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(22, '', 1, '0000-00-00', 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(23, '', 1, '0000-00-00', 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

-转储表的索引

-表statistik的索引

ALTER TABLE `statistik`
  ADD PRIMARY KEY (`id`),
  ADD KEY `stastus` (`stastus`);

-转储表的AUTO_INCREMENT

-表statistik的AUTO_INCREMENT

ALTER TABLE `statistik`
  MODIFY `id` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24;

2 个答案:

答案 0 :(得分:2)

最上面的嵌套选择只能返回一个值。它根本不应该是嵌套查询。最好在每个金额中添加一个案例声明

(SUM(case when stastus = '1' then A+AA+AB+AC+AD+AE+AF+AG+AH+AI+AJ+AK+AL+AM+AN+AO+
    B+C+CL+D+E+F+G+H+I+J+K+L+LK+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z else 0 end) 
    AS SELESAI,

并使所有其他格式都使用此格式。

sum(case when stastus='4' then A else 0 end) as A

否则,您可以使用联接或联合的多个查询来重构。

答案 1 :(得分:1)

尽管我更喜欢@TomC的方法,但您可以使用相关子查询而不是group by来完成所需的工作:

SELECT (SELECT SUM(A+AA+AB+AC+AD+AE+AF+AG+AH+AI+AJ+AK+AL+AM+AN+AO+
        B+C+CL+D+E+F+G+H+I+J+K+L+LK+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z) 
                  ) AS SELESAI
        FROM statistik s2
        WHERE s2.status = 1 
              s2.kategori = s.kategori
       ),
       . . .
FROM statistik s
. . .