简体   繁体   English

mysql 中“on 子句”中的未知列“column_name”

[英]Unknown column 'column_name' in 'on clause' in mysql

I have two table like bellow我有两张桌子如下

CREATE TABLE "accounts" (
    "name"      TEXT,
    "number"    INTEGER,
    "normal"    INTEGER
)

and

CREATE TABLE "transactions"
  (
     "id"        INTEGER, 
     "date"      TEXT,
     "amount"    REAL,
     "account"   INTEGER,
     "direction" INTEGER
  ) 

when I run this query it shown Unknown column 'a' in 'on clause'当我运行此查询时,它在“on 子句”中显示未知列“a”

Query is -查询是 -

select
   ((account / 100) * 100) as a,
   name,
   sum(amount * direction * normal) as balance
 from
   transactions
   left join accounts on a = accounts.number
 group by
   name
order by
  a,
  name;

I tried a lot by changing queries and unable to solve that.我通过更改查询进行了很多尝试,但无法解决该问题。

You can't refer to a column alias in the ON or WHERE clauses in the same query.您不能在同一查询的ONWHERE子句中引用列别名。 So you need to repeat the expression.所以你需要重复表达。

select
   ((account / 100) * 100) as a,
   name,
   sum(amount * direction * normal) as balance
 from
   transactions
   left join accounts on a = ((account / 100) * 100)
 group by
   name
order by
  a,
  name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM