SQL中 group by 1, order by 1 语句是什么意思

最近学习数据库,在 codecademy中, 遇到如下语句
SELECT a.dep_month,
       a.dep_day_of_week,
       AVG(a.flight_count) AS average_flights
  FROM (
        SELECT dep_month,
              dep_day_of_week,
               dep_date,
               COUNT(*) AS flight_count
          FROM flights
         GROUP BY 1,2,3
       ) a
 GROUP BY 1,2
 ORDER BY 1,2;


看到group by 1,2 和 order by 1, 2。看不懂,google,搜到了Stack Overflow 上有回答 What does SQL clause “GROUP BY 1” mean? 

大概意思就是,group by, order by 后面跟数字,指的是 select 后面选择的列(属性),1 代表第一个列(属性),依次类推。

因而上面最外层的

 GROUP BY 1,2
 ORDER BY 1,2
等价于

 GROUP BY a.dep_month, a.dep_day_of_week
 ORDER BY a.dep_month, a.dep_day_of_week

注意,这边从1开始数,而不是0

你可能感兴趣的:(SQL中 group by 1, order by 1 语句是什么意思)