sql >> Base de Datos >  >> RDS >> Mysql

Consulta SQL de error:use el método SUM, cuando # 1054 - Columna desconocida 'tbl_customers.id' en 'cláusula where'

Estabas usando tbl_customers en la subconsulta a la que no tenías acceso. Solo tenías que unirte en lugar de usar where:

SELECT tbl_customers.*,(SELECT SUM(amount) As Amount
FROM 
(
    SELECT tcc.entry_fees*COUNT(tccc.match_contest_id) as amount 
    FROM `tbl_cricket_customer_contests` tccc 
    JOIN `tbl_customers` ON (tccc.customer_id = tbl_customers.id)
    LEFT JOIN tbl_cricket_contest_matches tccm on(tccm.id=tccc.match_contest_id) 
    LEFT JOIN tbl_cricket_contests tcc ON (tcc.id=tccm.contest_id) 
    GROUP BY tccc.match_contest_id
) As DT) as spendamount
FROM (`tbl_customers`) 
WHERE `tbl_customers`.`is_deleted` = 'N' 
GROUP BY `tbl_customers`.`id` 
ORDER BY `spendamount` DESC