大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

南鱼

果然,我没有抓住重点

导航

PostgreSQL 基本语法

PostgreSQL 基本语法

PostgreSQL 基本语法

这里记录些PostgreSQL的基本语法,内容基本来自 [PostgreSQL 9.5.3 中文手册]

创建数据库

CREATE DATABASE mydb;
  • 1

删除数据库

DROP DATABASE mydb;
  • 1

创建表

CREATE TABLE weather (
    city            varchar(80),
    temp_lo         int,           -- 最低温度
    temp_hi         int,           -- 最高温度
    prcp            real,          -- 湿度 real是一种用于存储单精度浮点数的类型
    date            date
);
CREATE TABLE cities (
    name            varchar(80),
    location        point
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

删除表

DROP TABLE cities;
DROP TABLE weather
  • 1
  • 2

在表中增加行

INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) 
    VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
INSERT INTO weather (date, city, temp_hi, temp_lo) 
    VALUES ('1994-11-29', 'Hayward', 54, 37);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查询

SELECT * FROM weather;
SELECT * FROM cities;
SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
--可以在选择列表中写任意表达式
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
--检索旧金山的下雨天的天气:
SELECT * FROM weather WHERE city = 'San Francisco' AND prcp > 0.0;
--要求返回的查询结果是排好序的
SELECT * FROM weather ORDER BY city;
SELECT * FROM weather ORDER BY city, temp_lo;
--消除重复的行:
SELECT DISTINCT city FROM weather;
SELECT DISTINCT city FROM weather ORDER BY city;
--连接查询,一个同时访问同一个或者不同表的多个行的查询叫连接查询。
SELECT * FROM weather, cities WHERE city = name;
SELECT city, temp_lo, temp_hi, prcp, date, location
    FROM weather, cities
    WHERE city = name;
SELECT weather.city, weather.temp_lo, weather.temp_hi,
       weather.prcp, weather.date, cities.location
    FROM weather, cities
    WHERE cities.name = weather.city;
--使用内连接查询
SELECT * FROM weather INNER JOIN cities ON (weather.city = cities.name);
--观察结果集没有城市Hayward的结果行。这是因为连接查询会忽略各表中不匹配行
--如果需要显示表中不匹配行,那就使用外连接查询
SELECT * FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
--这个查询是一个左外连接, 因为在连接操作符左部的表中的行在输出中至少要出现一次, 而在右部的表的行只有在能找到匹配的左部表行是才被输出。如果输出的左部表的行没有对应匹配的右部表的行,那么右部表行的列将填充空值(null)。
--我们也可以把一个表和自己连接起来。这叫做自连接
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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

聚集函数

在一个行集合上计算count(计数)、sum(和)、avg(均值)、max(最大值)和min(最小值)的函数。

--查询所有记录中最低温度中的最高温度
SELECT max(temp_lo) FROM weather;
--查询上面的值发生在哪个城市
SELECT city FROM weather
    WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
--获取每个城市观测到的最低温度的最高值
SELECT city, max(temp_lo) FROM weather
    GROUP BY city;
--用HAVING 过滤这些被分组的行
SELECT city, max(temp_lo) FROM weather
    GROUP BY city
    HAVING max(temp_lo) < 40; 
--只给出那些所有temp_lo值都低于40的城市。并且只查询那些名字以"S"开头的城市
SELECT city, max(temp_lo) FROM weather
    WHERE city LIKE 'S%'
    GROUP BY city
    HAVING max(temp_lo) < 40; 

posted on 2018-07-12 23:25  南鱼羁荒渡  阅读(379)  评论(0)    收藏  举报