I have a query that runs fine, when I try to add one more column to where clouses it cannot find the column and gives an error.
SELECT '1' AS `row_count`, (
SELECT
COUNT(*)
FROM
`attendances`
WHERE `program_sessions`.`id` = `attendances`.`program_session_id`
AND `attendances`.`deleted_at` IS NULL
) AS `attendances_count`
FROM
`program_sessions`
LEFT JOIN `programs` ON `programs`.`id` = `program_sessions`.`program_id`
LEFT JOIN `program_categories` ON `program_categories`.`id` = `programs`.`program_category_id`
LEFT JOIN `service_areas` ON `service_areas`.`id` = `program_categories`.`service_area_id`
LEFT JOIN `locations` ON `locations`.`id` = `programs`.`location_id`
WHERE (
LOWER(`program_categories`.`name`) LIKE "%3%" OR
LOWER(`programs`.`name`) LIKE "%3%" OR
LOWER(`locations`.`name`) LIKE "%3%" OR
(attendances_count = 3) OR
LOWER(`service_areas`.`name`) LIKE "%3%"
)
AND `program_sessions`.`deleted_at` IS NULL
MySQL said:
#1054 - Unknown column 'attendances_count' in 'where clause'
The query somehow cannot reach the attendances_count
. What is it that I am doing wrong?