SELECT users.users_id,
users.name,
user_dolg.dogovor_name,
SUM(
CASE
WHEN user_dolg.hours = 'нет'
THEN 0
ELSE CAST(user_dolg.hours AS UNSIGNED)
END
) AS hoursSum
FROM users
INNER JOIN user_dolg ON user_dolg.user_id = users.users_id
WHERE hours > 0
AND user_dolg.date BETWEEN '2020-01-01' AND '2021-01-01'
GROUP BY users.users_id,
users.name,
user_dolg.dogovor_name
SELECT users.users_id,
users.name,
user_dolg.dogovor_name,
SUM(user_dolg.hours) AS hoursSum
FROM users
INNER JOIN user_dolg ON user_dolg.user_id = users.users_id
WHERE hours > 0
AND user_dolg.date BETWEEN '2020-01-01' AND '2021-01-01'
GROUP BY users.users_id,
users.name,
user_dolg.dogovor_name
SELECT users.users_id,
MAX(users.name) AS userName,
user_dolg.dogovor_name,
SUM(user_dolg.hours) AS hoursSum
FROM users
INNER JOIN user_dolg ON user_dolg.user_id = users.users_id
WHERE hours > 0
AND user_dolg.date BETWEEN '2020-01-01' AND '2021-01-01'
GROUP BY users.users_id,
user_dolg.dogovor_name
SELECT users.users_id,
MAX(users.name) AS userName,
user_dolg.dogovor_name,
SUM(user_dolg.hours) AS hoursSum
FROM users
INNER JOIN user_dolg ON user_dolg.user_id = users.users_id
WHERE hours > 0 BETWEEN '2020-01-01' AND '2021-01-01'
GROUP BY users.users_id,
user_dolg.dogovor_name
WHERE hours > 0 BETWEEN '2020-01-01' AND '2021-01-01'