i have query
SELECT p.name, (SELECT GROUP_CONCAT(DISTINCT CONCAT('', f.name) SEPARATOR ' , ') FROM edu_detail_facility df INNER JOIN edu_facility f ON df.id_edu_facility = f.id WHERE df.id_edu_provider = p.id) as Fasilitas, (SELECT GROUP_CONCAT(DISTINCT CONCAT('', a.name) SEPARATOR ' , ') FROM edu_detail_activity da INNER JOIN edu_activity a on da.id_edu_activity = a.id WHERE da.id_edu_provider = p.id) as Aktifitas FROM eduprovider p INNER JOIN edu_lot el ON p.id_school_lot = el.id INNER JOIN edu_type_of_school et ON p.id_school_tos = et.id INNER JOIN edu_curriculum ec ON p.id_curriculum = ec.id INNER JOIN city c ON p.city = c.id INNER JOIN province pr ON pr.id = p.province WHERE p.id_school_lot = '6' AND p.id_school_tos = '4' AND p.id_curriculum = '5' AND p.city = '151' AND p.province = '6' AND Aktifitas IN (1,2,3,4,5,6,7,8) AND Fasilitas IN (1,2,3,4,5) GROUP BY p.id
but mysql showing this error:
Unknown column 'Aktifitas' in 'where clause'
why mysql Alias cant be use in Where clause, but is there something I missing?
Thanks in advance.
Advertisement
Answer
You can’t use column’s alias in where clause you must repeat the code
(because the where clause is executed before of the select clause and for this reason the alias name is not know by the where clause )
SELECT p.name, (SELECT GROUP_CONCAT(DISTINCT CONCAT('', f.name) SEPARATOR ' , ') FROM edu_detail_facility df INNER JOIN edu_facility f ON df.id_edu_facility = f.id WHERE df.id_edu_provider = p.id) as Fasilitas, (SELECT GROUP_CONCAT(DISTINCT CONCAT('', a.name) SEPARATOR ' , ') FROM edu_detail_activity da INNER JOIN edu_activity a on da.id_edu_activity = a.id WHERE da.id_edu_provider = p.id) as Aktifitas FROM eduprovider p INNER JOIN edu_lot el ON p.id_school_lot = el.id INNER JOIN edu_type_of_school et ON p.id_school_tos = et.id INNER JOIN edu_curriculum ec ON p.id_curriculum = ec.id INNER JOIN city c ON p.city = c.id INNER JOIN province pr ON pr.id = p.province WHERE p.id_school_lot = '6' AND p.id_school_tos = '4' AND p.id_curriculum = '5' AND p.city = '151' AND p.province = '6' AND (SELECT GROUP_CONCAT(DISTINCT CONCAT('', a.name) SEPARATOR ' , ') FROM edu_detail_activity da INNER JOIN edu_activity a on da.id_edu_activity = a.id WHERE da.id_edu_provider = p.id) IN (1,2,3,4,5,6,7,8) AND (SELECT GROUP_CONCAT(DISTINCT CONCAT('', f.name) SEPARATOR ' , ') FROM edu_detail_facility df INNER JOIN edu_facility f ON df.id_edu_facility = f.id WHERE df.id_edu_provider = p.id) IN (1,2,3,4,5) GROUP BY p.id
eventually you can filter the result using having
SELECT p.name, (SELECT GROUP_CONCAT(DISTINCT CONCAT('', f.name) SEPARATOR ' , ') FROM edu_detail_facility df INNER JOIN edu_facility f ON df.id_edu_facility = f.id WHERE df.id_edu_provider = p.id) as Fasilitas, (SELECT GROUP_CONCAT(DISTINCT CONCAT('', a.name) SEPARATOR ' , ') FROM edu_detail_activity da INNER JOIN edu_activity a on da.id_edu_activity = a.id WHERE da.id_edu_provider = p.id) as Aktifitas FROM eduprovider p INNER JOIN edu_lot el ON p.id_school_lot = el.id INNER JOIN edu_type_of_school et ON p.id_school_tos = et.id INNER JOIN edu_curriculum ec ON p.id_curriculum = ec.id INNER JOIN city c ON p.city = c.id INNER JOIN province pr ON pr.id = p.province WHERE p.id_school_lot = '6' AND p.id_school_tos = '4' AND p.id_curriculum = '5' AND p.city = '151' AND p.province = '6' HAVING Aktifitas IN (1,2,3,4,5,6,7,8) AND Fasilitas IN (1,2,3,4,5) GROUP BY p.id