程序加载语句:
sql">C:\Users\49161>E:\sql1\bin\psql.exe -U postgres
输出表里的两列内容:
sql">postgres=# SELECT product_id,product_name
postgres-# FROM Product;
查询表里的所有列:*
sql">postgres=# SELECT *
postgres-# FROM Product;
格式:
空行:不可以
换行:可以
设定别名:
sql">postgres=# SELECT product_id AS id,
postgres-# product_name AS name,
postgres-# purchase_price AS price
postgres-# FROM Product
postgres-# ;
设定成中文:
sql">postgres=# SELECT product_id AS "商品编号",
postgres-# product_name AS "商品名称",
postgres-# purchase_price AS "进货单价"
postgres-# FROM Product;
商品编号 | 商品名称 | 进货单价
----------+----------+----------
0001 | T恤衫 | 500
0002 | 打孔器 | 320
0003 | 运动T恤 | 2800
0004 | 菜刀 | 2800
0005 | 高压锅 | 5000
0006 | 叉子 |
0007 | 擦菜板 | 790
0008 | 圆珠笔 |
(8 行记录)
删除重复行:
sql">postgres=# SELECT DISTINCT product_type
postgres-# FROM Product;
product_type
--------------
衣服
办公用品
厨房用具
(3 行记录)
对NULL数据同样有效
删除多列时,先将多列合并成一组数据,再执行删除操作
sql">postgres=# SELECT DISTINCT product_type,regist_date
postgres-# FROM Product
postgres-# ;
product_type | regist_date
--------------+-------------
办公用品 | 2009-09-20
厨房用具 | 2009-01-15
办公用品 | 2009-11-11
衣服 |
厨房用具 | 2008-04-28
厨房用具 | 2009-09-20
衣服 | 2009-09-20
(7 行记录)
WHERE 条件表达式做筛选
sql">postgres=# SELECT product_name,product_type
postgres-# FROM Product
postgres-# WHERE product_type='衣服';
product_name | product_type
--------------+--------------
T恤衫 | 衣服
运动T恤 | 衣服
(2 行记录)
也可以提取不作为查询条件的列:
sql">postgres=# SELECT product_name
postgres-# FROM Product
postgres-# WHERE product_type='衣服';
product_name
--------------
T恤衫
运动T恤
(2 行记录)
注意事项:
书写顺序,where写在后面
注释:
单行:–
多行:/* */
2.17计算表达式:
sql">postgres=# SELECT product_name,sale_price,
postgres-# sale_price*2 AS "sale_price_*2"
postgres-# FROM Product;
product_name | sale_price | sale_price_*2
--------------+------------+---------------
T恤衫 | 1000 | 2000
打孔器 | 500 | 1000
运动T恤 | 4000 | 8000
菜刀 | 3000 | 6000
高压锅 | 6800 | 13600
叉子 | 500 | 1000
擦菜板 | 880 | 1760
圆珠笔 | 100 | 200
(8 行记录)
注意:
带NULL的运算结果都是NULL
2.18选取售价为500的记录:
sql">postgres=# SELECT product_name,product_type
postgres-# FROM Product
postgres-# WHERE sale_price=500;
product_name | product_type
--------------+--------------
打孔器 | 办公用品
叉子 | 厨房用具
(2 行记录)
2.19选售价不为500的记录:
sql">postgres=# SELECT product_name,product_type
postgres-# FROM Product
postgres-# WHERE sale_price<>500;
product_name | product_type
--------------+--------------
T恤衫 | 衣服
运动T恤 | 衣服
菜刀 | 厨房用具
高压锅 | 厨房用具
擦菜板 | 厨房用具
圆珠笔 | 办公用品
(6 行记录)
2.21
日期也可以用比较运算符:
在2009.09.27之前的商品
sql">postgres=# SELECT product_name,product_type,regist_date
postgres-# FROM Product
postgres-# WHERE regist_date<'2009-09-27';
product_name | product_type | regist_date
--------------+--------------+-------------
T恤衫 | 衣服 | 2009-09-20
打孔器 | 办公用品 | 2009-09-20
菜刀 | 厨房用具 | 2009-09-20
高压锅 | 厨房用具 | 2009-01-15
叉子 | 厨房用具 | 2009-09-20
擦菜板 | 厨房用具 | 2008-04-28
(6 行记录)
2.22WHERE语句也可以用条件表达式:
sql">postgres=# SELECT product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# WHERE sale_price -purchase_price>500;
product_name | sale_price | purchase_price
--------------+------------+----------------
运动T恤 | 4000 | 2800
高压锅 | 6800 | 5000
(2 行记录)
2.24字符串表中选择大于’2‘的数据:
sql">postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# INSERT INTO Chars VALUES('1');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('2');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('3');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('11');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('12');
INSERT 0 1
postgres=# INSERT INTO Chars VALUES('222');
INSERT 0 1
postgres=# COMMIT;
COMMIT
postgres=# SELECT chr
postgres-# FROM Chars
postgres-# WHERE chr>'2';
chr
-----
3
222
(2 行记录)
2.28选取NULL记录:
sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price IS NULL;
product_name | purchase_price
--------------+----------------
叉子 |
圆珠笔 |
(2 行记录)
2.29选取不为NULL的记录:
sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price IS NOT NULL;
product_name | purchase_price
--------------+----------------
T恤衫 | 500
打孔器 | 320
运动T恤 | 2800
菜刀 | 2800
高压锅 | 5000
擦菜板 | 790
(6 行记录)
注意:
1.比较运算符,相等,用单等号
不相等:<>
2.比较运算符不能判断是否为NULL
3.判断NULL,用IS NULL语句
4.字符串是按照字典顺序排序的:
1 11 12 2 222 3
逻辑运算符:
NOT表否定
AND并
OR或
2.33AND语句
sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE product_type='厨房用具'
postgres-# AND sale_price>=3000;
product_name | purchase_price
--------------+----------------
菜刀 | 2800
高压锅 | 5000
(2 行记录)
2.34OR语句
sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE product_type='厨房用具'
postgres-# OR sale_price >=3000;
product_name | purchase_price
--------------+----------------
运动T恤 | 2800
菜刀 | 2800
高压锅 | 5000
叉子 |
擦菜板 | 790
(5 行记录)
2.36括号的优先级最高:
sql">postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE product_type='办公用品'
postgres-# AND (regist_date='2009-09-11'
postgres(# OR regist_date='2009-09-20');
product_name | purchase_price
--------------+----------------
打孔器 | 320
(1 行记录)
注意:AND优先级高于OR
sql是三值逻辑,还有UNKNOWN不确定
练习2.4
sql">postgres=# SELECT product_name,product_type,
postgres-# sale_price*9/10-purchase_price AS profit
postgres-# FROM Product
postgres-# WHERE sale_price*0.9-purchase_price >100
postgres-# AND (product_type='办公用品')
postgres-# ;
product_name | product_type | profit
--------------+--------------+--------
打孔器 | 办公用品 | 130
(1 行记录)
and和or的书写格式;新建列名;条件判断不用列名