需要 SQL Query 帮助并显示错误消息子查询返回超过 1 行
Posted
技术标签:
【中文标题】需要 SQL Query 帮助并显示错误消息子查询返回超过 1 行【英文标题】:Need assistance with SQL Query with error message Subquery returns more than 1 row 【发布时间】:2020-10-09 08:54:34 【问题描述】:我有两张表,我想通过根据匹配的 voteid 检索分配(预算)来更新费用的运行余额
Apache/2.4.43 (Win64) OpenSSL/1.1.1g PHP/7.4.6 数据库客户端版本:libmysql - mysqlnd 7.4.6 PHP 扩展:mysqli 文档 curl 文档 mbstring 文档 PHP 版本:7.4.6 服务器类型:MariaDB这是我目前的 SQL 语句
SELECT allocation
FROM vote
INNER JOIN expenditure ON vote.voteid = expenditure.voteid
GROUP BY vote.voteid;
SET @csum = (SELECT allocation
FROM vote
INNER JOIN expenditure ON vote.voteid = expenditure.voteid
GROUP BY vote.voteid);
UPDATE expenditure
SET balance = (@csum = @csum - expenses) OVER (partition BY voteid ORDER BY expenditureid) AS balance;
错误信息:
子查询返回多于 1 行
表 1
| voteid | allocation |
+--------+------------+
| 1 | 50000 |
| 2 | 10000 |
| 3 | 34000 |
| 4 | 70000 |
表 2
| expenditureid | voteid | expenses |
+---------------+--------+----------+
| 1 | 2 | 300 |
| 2 | 2 | 650 |
| 3 | 4 | 900 |
| 4 | 4 | 1200 |
| 5 | 3 | 34000 |
预期结果
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| expenditureid | voteid | expenses | balance (running difference) | calculation (sample column should not be included) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| | | | | 10000 |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| 1 | 2 | 300 | 9700 | (10000-300) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| 2 | 2 | 650 | 9050 | (9700-650) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| | | | | |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| | | | | 70000 |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| 3 | 4 | 900 | 69100 | (70000-900) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| 4 | 4 | 1200 | 67900 | (69100-1200) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| | | | | |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| | | | | 34000 |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
| 5 | 3 | 4000 | 30000 | (34000-4000) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+
【问题讨论】:
您的示例数据和计算毫无意义。我不知道balance
是如何计算的。
我是这个论坛的新手,所以请多多包涵。您是否在手机或笔记本电脑上查看此内容,因为由于某种原因,表格无法在手机上正确显示?所以我可以理解为什么你可能不明白余额是如何计算的。你看清楚桌子了吗?如果你不明白,我可以解释得更好
。 .我不确定论坛与它有多大关系。您有一行 300 作为费用,余额为 7000,表达式为 10,000 - 7,000。这 7,000 人完全是个谜。
对,你是完全正确的。计算是我的错误。我刚刚更新了它
但是你对我想要的结果有更好的理解吗?
【参考方案1】:
total_allocation 不是您所显示的列。
你应该做的是:
SET @csum = (SELECT sum(vote.allocation) AS total_allocation
FROM vote
INNER JOIN expenditure ON vote.voteid = expenditure.voteid
GROUP BY vote.voteid);
【讨论】:
以上是关于需要 SQL Query 帮助并显示错误消息子查询返回超过 1 行的主要内容,如果未能解决你的问题,请参考以下文章
带有子查询的 SQL 查询上的 MS Access VBA 运行时错误 3075