Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL求和并返回多个其他行结果_Mysql_Sql_Sum - Fatal编程技术网

MySQL求和并返回多个其他行结果

MySQL求和并返回多个其他行结果,mysql,sql,sum,Mysql,Sql,Sum,我有以下MySQL表: 表:注册用户 ----------------- id | username ----------------- 1 a 2 b 3 c 表:条例草案 ----------------------- id | m12 | p12 | ----------------------- 1 13.69 1 2 0.00 1 3 269.89 1 我只得到一行结果

我有以下MySQL表:

表:注册用户

-----------------
  id | username 
-----------------
  1     a
  2     b
  3     c
表:条例草案

-----------------------
  id | m12     | p12 |
-----------------------
  1     13.69     1
  2     0.00      1
  3    269.89     1
我只得到一行结果,需要得到所有结果(3行结果,每行合计上个月),因此此行需要每行有283.58的值

使用GROUP BY我得到正确的total_prev_MOUNT值,但我只得到一行结果…在本例中,我需要3行结果,如:

------------------------------------------------
  payed | id     | username |  total_prev_month
------------------------------------------------
  1        1         a              283.58
  1        2         b              283.58
  1        3         c              283.58

只需在选择列表中使用子查询:

SELECT b.p12 AS payed, d.id, username, 
       (SELECT ROUND(SUM(m12),2) FROM `bill` ) AS total_prev_month 
  FROM `reg_users` d 
  JOIN `bill` b ON d.id = b.id 
 ORDER BY d.id

SUM(b.m12)OVER()作为上一个月的总数
窗口分析函数,例如
SUM(b.m12)OVER()作为上一个月的总数
可能用于DB 8.0版,您的DB版本是什么?DB版本是7.3…以上答案很好。谢谢您的帮助。
SELECT b.p12 AS payed, d.id, username, 
       (SELECT ROUND(SUM(m12),2) FROM `bill` ) AS total_prev_month 
  FROM `reg_users` d 
  JOIN `bill` b ON d.id = b.id 
 ORDER BY d.id