# 1. 聚集语法规则 聚集的语法规则各数据库有差异。 # 2. 投影列规则 在有Group By的情况下,投影列要么出现在Group By中,要么在聚集函数中,例如,以下SQL是合法的: ```sql select sum(a) from t group by a; select sum(a),a from t group by a; select sum(b),a from t group by a; select sum(a+b) from t group by a+b; select sum(a+b),a+b from t group by a+b; select a+b from t group by a+b; select a,b from t group by a,b; ``` 以下SQL是非合法的: ```sql -- 投影列b既不在聚集函数中,也不在Group By中: select sum(a),b from t group by a; select * from t group by a; select b from t group by a; select a,b from t group by a; select sum(a+b),a from t group by a+b; ``` 有些数据库,支持上述非法SQL,例如, ```sql select b from t group by a; ``` 因为a的每个分组中可能对应多个b值,返回结果b列显示第一个值。