【发布时间】:2019-05-07 03:25:21
【问题描述】:
我尝试运行具有以下条件的查询:
SELECT
`doctors`.*,
(
SELECT GROUP_CONCAT(`areas`.`areaName` SEPARATOR ', ')
FROM `areas_has_doctors`
INNER JOIN `areas` ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
) as `areas`,
(
SELECT GROUP_CONCAT(`areas`.`areaId`)
FROM `areas_has_doctors`
INNER JOIN `areas` ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
) as `areasIdies`
FROM
`cats_has_doctors`
INNER JOIN `doctors` ON `doctors`.`doctorId` = `cats_has_doctors`.`doctorId`
WHERE
`cats_has_doctors`.`catId` = '1' && `doctors`.`disable` = 0
GROUP BY
`cats_has_doctors`.`relationId`
HAVING FIND_IN_SET('1,2,3,4', `areasIdies`)
带有10.0.21-MariaDB - MariaDB 服务器的服务器正在工作
但在服务器中:
5.5.61-cll - MySQL Community Server (GPL)
我得到了错误:
Unknown column 'areasIdies' in 'having clause'
我能做什么?
【问题讨论】:
-
HAVING部分早于SELECT应用。将原始查询替换为别名。 -
"HAVING 部分早于 SELECT" @revo 不确定你的意思我假设你的意思是
HAVING不能使用来自同一个SELECT查询的别名?如果这就是你的意思,那你就错了。 -
基本上,
HAVING不能与别名相处...你需要指定一个HAVING + full subquery或将所有内容都包含在一个大查询中,然后使用 have in最终输出(可能会影响 mySQL 的优化计划) -
FIND_IN_SET倒退:针先来,草垛次之。或者您正在尝试不可能的事情:检查多个键。 -
HAVING条件中的反引号 (`) 的解释似乎存在错误,仅限 MySQL 5.5。见db-fiddle。