PostgreSQL笔记(SQL语言注意点)

连接查询
注意区别以下三种情况

SELECT *
    FROM weather, cities
    WHERE city = name;
SELECT *
    FROM weather 
    LEFT OUTER JOIN cities ON (weather.city = cities.name);
SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
    W2.city, W2.temp_lo AS low, W2.temp_hi AS high
    FROM weather W1, weather W2
    WHERE W1.temp_lo < W2.temp_lo
    AND W1.temp_hi > W2.temp_hi;

在聚集函数使用下WHERE 和 HAVING 的基本区别
WHERE 在分组和聚集计算之前选取输入行(它控制哪些行进入聚集计算),而 HAVING 在分组和聚集之后选取输出行。因此,WHERE 子句不能包含聚集函数;因为试图用聚集函数判断那些行将要输入给聚集运算是没有意义的。相反,HAVING 子句总是包含聚集函数。当然,你可以写不使用聚集的 HAVING 子句,但这样做没什么好处,因为同样的条件可以更有效地用于 WHERE 阶段。

Add a comment

Add comment

Fill out the form below to add your own comments

User data





Add your comment