First, you cannot use ALIAS on
the WHERE clause.
You should be using the column,
SELECT SUBSTRING(rating, INSTR(rating,',') +1, +2) AS val
FROM users
WHERE SUBSTRING(rating, INSTR(rating,',') +1, +2) = '15'
The reason is as follows: the order of operation is SQL,
- FROM clause
- WHERE clause
- GROUP BY clause
- HAVING clause
- SELECT clause
- ORDER BY clause
the ALIAS takes
place on the SELECT clause
which is before the WHERE clause.
if you really want to use the alias, wrap it in a subquery,
SELECT *
FROM
(
SELECT SUBSTRING(rating, INSTR(rating,',') +1, +2) AS val
FROM users
) TMP
WHERE val = '15'
Reference: http://stackoverflow.com/questions/14413867/mysql-select-as-in-where
本文解释了在SQL查询中为何不能在WHERE子句中直接使用别名,并提供正确的语法示例。介绍了如何通过子查询来解决这一问题。
9872

被折叠的 条评论
为什么被折叠?



