MySQL - 在 WHERE 中选择 AS

MySQL - SELECT AS in WHERE(MySQL - 在 WHERE 中选择 AS)
本文介绍了MySQL - 在 WHERE 中选择 AS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,这不起作用:

For some reason, this doesn't work:

select substring(rating, instr(rating,',') +1, +2) as val
from users where val = '15';

它给出了这个错误:

错误 1054 (42S22):where 子句"中的未知列val"

ERROR 1054 (42S22): Unknown column 'val' in 'where clause'

那我该怎么做?

推荐答案

首先,您不能在 WHERE 子句上使用 ALIAS.您应该使用该列,

First, you cannot use ALIAS on the WHERE clause. You shoul be using the column,

SELECT SUBSTRING(rating, INSTR(rating,',') +1, +2) AS val 
FROM   users 
WHERE  SUBSTRING(rating, INSTR(rating,',') +1, +2) = '15'

原因如下:操作顺序是SQL,

The reason is as follows: the order of operation is SQL,

  • FROM 子句
  • WHERE 子句
  • GROUP BY 子句
  • HAVING 子句
  • SELECT 子句
  • ORDER BY 子句

ALIAS 发生在 SELECT 子句上,它位于 WHERE 子句之前.

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
    ) s
WHERE   val  = '15'

这篇关于MySQL - 在 WHERE 中选择 AS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)