我有三桌餐,费用,用户!我想计算用户 一个月的用餐细节!我已经尝试过很多方法:(
我尝试使用user.id分组,但返回所有用户的总费用 与总费用相同!计算输出完全意外。
SELECT u.name ,
((SUM(tot_expenseamount)/ SUM(tot_noofmeal))*tot_noofmeal) as total_Cost,
IFNULL(t.tot_expenseamount,0) AS tot_expense_amount ,
IFNULL(n.tot_noofmeal,0) AS tot_no_of_meal
FROM users u
LEFT JOIN (
SELECT e.user_id , SUM(e.expenseamount) AS tot_expenseamount
FROM expenses e
WHERE e.expensedate >= '2019-04-01' + INTERVAL 0 MONTH AND e.expensedate < '2019-04-01' + INTERVAL 1 MONTH
GROUP BY e.user_id ) t ON t.user_id = u.id
LEFT JOIN (
SELECT m.user_id , SUM(m.noofmeal) AS tot_noofmeal
FROM meals m
WHERE m.mealdate >= '2019-04-01' + INTERVAL 0 MONTH AND m.mealdate < '2019-04-01' + INTERVAL 1 MONTH
GROUP BY m.user_id ) n ON n.user_id = u.id
GROUPBY u.name
ORDER BY u.name
我期望用户的总费用!! 总费用=进餐费用*用户总进餐; 进餐费用=总费用/所有用户总进餐的总和
示例:我有三个用户x,y,z,他们4月的总膳食为 10,20,30,费用为2000,1000,3000
现在总费用= 6000(2000 + 1000 + 3000)餐= 60(10 + 20 + 30)餐 费率=(6000/60)= 100.00
每个用户的总费用为x,总费用= 10 * 100 = 1000 / =
用餐表
用户表
费用表
预期输出