简体   繁体   English

MySQL结合选择总和和选择查询

[英]MySQL combine select sum and select query

I want to sum 4 cells for each row based on a previous query that will reduce the selection down to the important rows. 我想根据先前的查询为每行总计4个单元格,这将把选择减少到重要的行。

Basically I need to combine those two queries (which work on their own): 基本上,我需要结合这两个查询(它们可以独立工作):

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

I tried to replace "table" with the second query but it's probably not the right way, since I'm getting errors here. 我试图用第二个查询替换“表”,但这可能不是正确的方法,因为我在这里遇到错误。

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

You were supposed to create an alias name for the block replacing table in your first query. 您应该在第一个查询中为块替换table创建别名。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM