我正在尝试使用基于标量子查询结果的 WHERE 子句。查询在没有 WHERE 子句的情况下正确执行。使用 WHERE 子句,我得到错误代码:1054。“where 子句”中的未知列“available_services”。
如何实现基于子查询结果的过滤?
此外,由于子查询可能非常低效,因此有关如何改进查询的任何建议都会很有用。
SELECT DISTINCT
`suppliers`.`id` AS `supplier_id`,
`suppliers`.`name`,
`suppliers`.`code`,
`suppliers`.`notes`,
(
SELECT GROUP_CONCAT(
`services`.`name`
ORDER BY `services`.`order`
SEPARATOR ', '
)
FROM `supplier_services`
LEFT JOIN `services`
ON `supplier_services`.`service_id` = `services`.`id`
WHERE
`supplier_services`.`service_id` = `services`.`id`
AND `supplier_services`.`supplier_id` = `suppliers`.`id`
GROUP BY `supplier_services`.`supplier_id`
) AS `available_services`
FROM `suppliers`
WHERE `available_services` like '%pet%'
GROUP BY `suppliers`.`id`