MySQL结合了select sum和select查询

时间:2018-05-28 14:39:33

标签: mysql select sum

我希望根据之前的查询为每一行加上4个单元格,这会将选择减少到重要的行。

基本上我需要将这两个查询(它们自己工作)结合起来:

SELECT columnx, SUM(`column1`+ `column2` + `column3` + `column4`) as total 
                FROM table GROUP BY columnx


SELECT * FROM (SELECT * FROM table ORDER BY columny DESC LIMIT 5) t 
                ORDER BY CASE
                when `pos` = 'PG' then 1
                when `pos` = 'SG' then 2
                when `pos` = 'SF' then 3
                when `pos` = 'PF' then 4
                else 5
                end asc

我试图用第二个查询替换“table”,但它可能不是正确的方法,因为我在这里遇到错误。

SELECT columnx, SUM(`column1`+ `column2` + `column3` + `column4`) as total FROM 
                  (( SELECT * FROM (SELECT * FROM table ORDER BY columny DESC LIMIT 5) t 
                  ORDER BY CASE
                  when `pos` = 'PG' then 1
                  when `pos` = 'SG' then 2
                  when `pos` = 'SF' then 3
                  when `pos` = 'PF' then 4
                  else 5
                  end asc) 
               GROUP BY columnx

1 个答案:

答案 0 :(得分:1)

您应该在第一个查询中为替换SELECT u.columnx, SUM(u.`column1`+ u.`column2` + u.`column3` + u.`column4`) as total FROM (SELECT t.* FROM (SELECT * FROM table ORDER BY columny DESC LIMIT 5) t ORDER BY CASE WHEN t.`pos`='PG' THEN 1 WHEN t.`pos`='SG' THEN 2 WHEN t.`pos`='SF' THEN 3 WHEN t.`pos`='PF' THEN 4 ELSE 5 END ASC) u GROUP BY u.columnx 的块创建别名。

public static int countWords(char[] array) {
    boolean isSpace = true;
    int count = 0;

    for (int i=0; i < array.length; ++i) {
        if (array[i] != ' ' && isSpace) {
            ++count;
            isSpace = false;
        }
        else if (array[i] == ' ') {
            isSpace = true;
        }
    }

    return count;
}