0

我正在尝试提取所有具有工程师且总分为 16 或更高的记录。

value_money、quality_service、friendlyness、response_time 都是同一张表中的字段。

我正在尝试添加所有这些并执行 WHERE。

这是我到目前为止所拥有的

SELECT *,
value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%'
AND
overall < 16
ORDER BY date DESC
LIMIT 0, 15

这是我得到的错误。

#1054 - Unknown column 'overall' in 'where clause'
4

6 回答 6

2

您不能在 WHERE 子句中使用别名。你必须使用完整的value_money+quality_service+friendliness+response_time组合。

于 2013-09-19T15:42:09.677 回答
2

别名不能用于WHERE. 做任何一个

SELECT *,
value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%'
AND
value_money+quality_service+friendliness+response_time < 16
ORDER BY date DESC
LIMIT 0, 15

或者

SELECT * FROM 
(
SELECT *,
value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%'

)a WHERE overall < 16
ORDER BY date DESC
LIMIT 0,15
于 2013-09-19T15:42:18.003 回答
1

最简单的解决方案是将条件移至having子句:

SELECT *, value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%'
HAVING overall < 16
ORDER BY date DESC
LIMIT 0, 15;

这是一个 MySQL 扩展,不适用于其他数据库。

于 2013-09-19T15:55:59.887 回答
0

您的服务器可能不支持该功能。如果是这样的话,我可以想到两个选择。子查询:

SELECT * FROM (
SELECT *, value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%') AS t
WHERE overall < 16
ORDER BY date DESC
LIMIT 0, 15

或重复等式:

SELECT *,
value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%'
AND
value_money+quality_service+friendliness+response_time < 16
ORDER BY date DESC
LIMIT 0, 15
于 2013-09-19T15:45:17.407 回答
0

不确定您使用的是什么数据库,但请尝试以下操作:

SELECT *,
value_money+quality_service+friendliness+response_time AS overall
FROM feedback
WHERE engineer LIKE '%Chris Gauden%'
AND
SUM(value_money+quality_service+friendliness+response_time) < 16
ORDER BY date DESC
LIMIT 0, 15
于 2013-09-19T15:43:23.277 回答
0

由于总体不是该表中的列,因此您不能在where子句中引用它。

你可以这样做:

SELECT * FROM 
(
    SELECT *,
           value_money+quality_service+friendliness+response_time AS overall
    FROM feedback
    WHERE engineer LIKE '%Chris Gauden%'
) AS ComputedTable
WHERE
    overall < 16
ORDER BY date DESC
LIMIT 0, 15
于 2013-09-19T15:43:40.130 回答