簡體   English   中英

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

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

我有兩張桌子如下

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

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

當我運行此查詢時,它在“on 子句”中顯示未知列“a”

查詢是 -

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;

我通過更改查詢進行了很多嘗試,但無法解決該問題。

您不能在同一查詢的ONWHERE子句中引用列別名。 所以你需要重復表達。

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