У меня есть две таблицы, и я хотел бы обновить текущий баланс расходов, получив распределение (бюджет) на основе совпадающего идентификатора голосования
Я использую phpmyadmin mysql
- Apache / 2.4.43 (Win64) OpenSSL / 1.1.1g PHP / 7.4.6
- Версия клиента базы данных: lib mysql - mysqlnd 7.4.6
- PHP расширение : mysqli Documentation curl Documentation mbstring Documentation
- 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) |
+---------------+--------+----------+------------------------------+-----------------------------------------------------+