我有这些表。
我需要一个单个查询,该查询将获取指定级别(level_id)中所有学生的总付款明细,并显示每个学生的付款费用(使用学生level_id查找 payment_fees 表
我有这个查询:
select students.id, students.name name, levels.name class, MAX(payments.created_at) date, SUM(payments.amount) amount
from `students`
left join `payments` on `students`.`id` = `payments`.`student_id` and `payments`.`payment_type_id` = '1'
inner join `levels` on `levels`.`id` = `students`.`level_id`
group by `students`.`id`
这很好用Here is the GUI presentation (在页面按钮上检查现实生活中的查询)和Here is the json response,但我还需要一列总计这是学生级别和指定的payment_type_id的付款费用(来自Payment_fees 金额列),因此我可以将金额与总计进行比较,并了解学生是否已完成/是否付款。
我已经尝试过,但是正在处理此错误:
select students.id, students.name name, levels.name class, MAX(payments.created_at) date, SUM(payments.amount) amount, payment_fees.amount as total
from `students`
left join `payments` on `students`.`id` = `payments`.`student_id` and `payments`.`payment_type_id` = '1'
inner join `levels` on `levels`.`id` = `students`.`level_id`
inner join 'payment_fees' on 'payment_fees.level_id' = 'students.level_id'
但是它不起作用...给出了此错误:Here is the error response
SQLSTATE[42000]:
Syntax error or access violation: 1055 Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'schooladminer.payment_type_fees.amount' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
答案 0 :(得分:0)
您可以使用子查询
进行分隔select T3.student_name,T3.Level_name,T3.payment_name,T3.payment as student_payment,created_at,
T4.payment_name as Level_payment,LevelPayment from
(
select student_name,Level_name,payment_name,sum(amount) as payment,max(created_at) as created_at from
(
select S.name as student_name,S.id as student_id,
L.name as Level_name,L.level_id from students S
left join levels L
on S.level_id=L.id
)T1
left join
(
select PT.name as payment_name,id, student_id, payment_type_id, amount, created_at
from payments P
left join payment_types PT
on P.payment_type_id=PT.id
) T2
on T1.student_id=T2.student_id
group by student_name,Level_name,payment_name,T1.student_id
) T3
left join
(
select PT.name as payment_name,L.name as Level_name,l.level_id, payment_type_id, sum(amount) as LevelPayment from payment_fees PF
left join
payment_types PT
on PF.payment_type_id=PT.id
left join levels L on PF.level_id=L.id
group by PT.name,L.name,L.level_id,payment_type_id
) T4 on T3.Level_name=T4.Level_name