这是我的SQL查询:

SELECT (
    SELECT COUNT(appointment_doctor) FROM appointment
    JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
    WHERE appointment_doctor = doctor_id
) * doctor_payment_rate AS fees FROM doctor
JOIN doctor_payment ON doctor.doctor_id=doctor_payment.doctor_payment_doctor


结果:


  费用500 1000 615 11615 1160 615 1610 285 660 220 3835 215 615 555 555 555 1065 1110 620 445 220 660 625 60 1715 615 10 115 615 60 625 60 330 615 625 720 625 615 670 615 500 615 500


我希望所有这些值的总和为total_fee

最佳答案

试试这个查询:

SELECT SUM (
    (SELECT COUNT(appointment_doctor) FROM appointment
    JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
    WHERE appointment_doctor = doctor_id) * doctor_payment_rate))
    AS total_fee
    FROM doctor
    JOIN doctor_payment
    ON doctor.doctor_id=doctor_payment.doctor_payment_doctor

关于mysql - MySQL查询从另一个查询的结果中找到总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29553316/

10-14 14:45