我有一个这样的查询:
SELECT
date(created_at as date) as created_date,
id,
count(distinct file_name) as files_total,
null as files_error
FROM table
GROUP BY created_date, id
我的表中没有名为
created_date
的列。为什么这个查询有效?
Created_Date
在查询的选择部分中以创建日期创建。
SQL按以下顺序处理:
from, where, group by, having, select, order by
Group By
先于Select
。在上面的查询中,别名直到g
roup by
之后才出现,但是由于某种原因,别名在group by
中工作。这是为什么?
最佳答案
您没有名为created_date
的列,但在此处创建了别名created_date
date(created_at as date) as created_date,
在postgresql中,别名可以在
Group by
中引用,这就是它工作的原因逻辑上
Group by
子句可以在select
之后处理,尽管在所有DBMS
中并非如此