The needs:
- get a list with the names of the students that paid for a book in 2024
- get the total they paid for their book(s)
I have the following request:
SELECT id_student, name_student, date_insc_student, payment_student
FROM tblstud
LEFT JOIN
( SELECT SUM(amount_payment) AS totalpay, idstud_payment FROM $t_p tblpay GROUP BY idstud_payment ) AS tblpay
ON tblpay.idstud_payment = tblstud.id_student
WHERE year(tblstud.date_insc_student) = 2024
ORDER BY date_insc_student, name_student
I get the data I need and read line by line, but there is no totalpay in the result. How do I get the total amount of what each student paid for his books?
I've read some SO questions such as this and this, and some others, but no clue. What am I doing wrong/missing?
I can also use another differently structured query if needed.
The needs:
- get a list with the names of the students that paid for a book in 2024
- get the total they paid for their book(s)
I have the following request:
SELECT id_student, name_student, date_insc_student, payment_student
FROM tblstud
LEFT JOIN
( SELECT SUM(amount_payment) AS totalpay, idstud_payment FROM $t_p tblpay GROUP BY idstud_payment ) AS tblpay
ON tblpay.idstud_payment = tblstud.id_student
WHERE year(tblstud.date_insc_student) = 2024
ORDER BY date_insc_student, name_student
I get the data I need and read line by line, but there is no totalpay in the result. How do I get the total amount of what each student paid for his books?
I've read some SO questions such as this and this, and some others, but no clue. What am I doing wrong/missing?
I can also use another differently structured query if needed.
Share Improve this question edited Feb 6 at 16:16 Your Common Sense 158k42 gold badges221 silver badges364 bronze badges asked Feb 6 at 15:29 BookKeeperBookKeeper 114 bronze badges New contributor BookKeeper is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 10 | Show 5 more comments2 Answers
Reset to default 2If you want all students, regardless of whether they paid for 2024, you can keep the LEFT JOIN
and do:
SELECT s.id_student, s.name_student, s,date_insc_student, s,payment_student,
SUM(tblpay.amount_payment) as totalpay
FROM tblstud s
LEFT JOIN FROM $t_p tblpay on tblpay.idstud_payment = s.id_student and year(tblstud.date_insc_student) = 2024
GRPUP BY s.id_student
ORDER BY s.date_insc_student, s.name_student
If you want only those who paid for 2024, you can do:
SELECT s.id_student, s.name_student, s,date_insc_student, s,payment_student,
SUM(tblpay.amount_payment) as totalpay
FROM tblstud s
JOIN FROM $t_p tblpay on tblpay.idstud_payment = s.id_student
WHERE year(tblstud.date_insc_student) = 2024
GRPUP BY s.id_student
ORDER BY s.date_insc_student, s.name_student
totalpay
is available from the joined query, you can add it to your select list:
SELECT id_student, name_student, date_insc_student, payment_student, totalpay
-- Here --------------------------------------------------------------^
FROM tblstud
LEFT JOIN
( SELECT SUM(amount_payment) AS totalpay, idstud_payment FROM $t_p tblpay GROUP BY idstud_payment ) AS tblpay
ON tblpay.idstud_payment = tblstud.id_student
WHERE year(tblstud.date_insc_student) = 2024
ORDER BY date_insc_student, name_student
totalpay
in your select statement. – aynber Commented Feb 6 at 15:35$pdo->prepare
(unless there's another form of missing context), but rather the code that attempts to read the results, especially the part that tries to read the (missing)totalpay
column. – John Bollinger Commented Feb 6 at 15:40$t_e
table doesn't have a column namedtotalpay
, then you can just add it afterpayment_student
. If it does, then you'll need to use the table aliastblpay.totalpay
afterpayment_student
. – aynber Commented Feb 6 at 15:40SELECT id_student, name_student, (SELECT SUM(amount_payment) FROM tblpay WHERE idstud_payment = id_student) AS totalpay FROM tblstud WHERE year(date_insc_student) = 2024
– Olivier Commented Feb 6 at 16:03