Home  >  Q&A  >  body text

MYSQL中如何把SELECT A AS B中的B作为WHERE筛选条件

比如SELECT a AS b WHRER b=1;

我这样使用会报错,说b不存在。

PHPzPHPz2742 days ago639

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-17 15:38:18

    Because when running SQL statements at the bottom of mysql: the filtering conditions after where come first, and the alias of as B comes after.
    So when the machine sees the alias after where, it does not recognize it, so it will report that B does not exist.

    If you must use B as the filter condition:
    Solution: Nest another layer outside.
    select * from
    (
    select A as B from table
    ) t
    where t.B = XXX -- arbitrary filter conditions

    If there is no nesting, you can only use A as the filter condition

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:38:18

    Of course it does not exist, this b is only for an alias with the query result of a
    select a AS b FROM table where a = 1

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:38:18

    select t.b from
    (

    select a as B from table

    ) t

    where t.b =xxxx

    B at this time can be used directly in where

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:38:18

    b is the alias for query result a and it definitely does not exist in where

    reply
    0
  • Cancelreply