David Bravo David Bravo - 1 year ago 59
MySQL Question

Column not found in where with inner joins

I have this mysql query:

select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`sumaTotal` >= 100 and `sumaTotal` <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc

Mysql says: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sumaTotal' in 'where clause'

Please help I think that I can use more than one select o some, but don't know how...

Answer Source

In your query sumaTotal is an alias of SUM(sd_documentos.total). So either use HAVING sumaTotal >= 100 and sumaTotal <= 200 or WHERE SUM(sc_documentos.total) >= 100 AND SUM(sc_documentos.total) <= 200