i want count the hours of our members, where they was helping us. The table seems like this:
CREATE TABLE `stunden` (
`std_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`einsatz_id` int(11) NOT NULL,
`stunden` decimal(5,1) NOT NULL DEFAULT '0.0',
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`std_id`)
) ENGINE=InnoDB;
INSERT INTO stunden VALUES
(1,1,2,1.0,'2018-01-19 00:36:15'),
(2,2,2,1.0,'2018-01-19 00:36:15'),
(3,4,2,1.0,'2018-01-19 00:36:15'),
(4,1,1,2.0,'2018-01-19 00:36:37'),
(5,5,1,2.0,'2018-01-19 00:36:37'),
(6,1,2,2.5,'2018-01-20 12:00:36');
mysql> select * from stunden;
+--------+---------+------------+---------+---------------------+
| std_id | user_id | einsatz_id | stunden | last_update |
+--------+---------+------------+---------+---------------------+
| 1 | 1 | 2 | 1.0 | 2018-01-19 00:36:15 |
| 2 | 2 | 2 | 1.0 | 2018-01-19 00:36:15 |
| 3 | 4 | 2 | 1.0 | 2018-01-19 00:36:15 |
| 4 | 1 | 1 | 2.0 | 2018-01-19 00:36:37 |
| 5 | 5 | 1 | 2.0 | 2018-01-19 00:36:37 |
| 6 | 1 | 2 | 2.5 | 2018-01-20 12:00:36 |
+--------+---------+------------+---------+---------------------+
6 rows in set (0,00 sec)
You can see that member with user_id = 1, was available two times at einsatz_id=2 and one times at einsatz_id = 1. What i need is the sum of stunden (stunden=hours) per einsatz_id and per user_id. I have tried with the following query.
SELECT
(SELECT SUM(s.stunden) AS Zeit FROM stunden s GROUP BY s.user_id, s.einsatz_id) AS STUNDEN,
m.nachname,
m.vorname,
a.einsatz
FROM
stunden s,
mitglieder m,
arbeitseinsatz a
WHERE
s.user_id = m.id
AND s.einsatz_id = a.einsatz_id
GROUP BY s.user_id, s.einsatz_id
ORDER BY m.nachname , m.vorname ASC
It results in the following error message:
"ERROR 1242 (21000): Subquery returns more than 1 row".
Any help would be appreciated.