PostgreSQL数据查询

employee表:

eid [PK] ename eage esalary esex eemail
integer character varying(20) integer integer character varying(1) character varying(32)

PostgreSQL数据查询–详细速查手册

一、基本查询语句

1.1 SELECT语句的基本格式:

SELECT
    {* | <字段列表>}
    [
        {FROM <表1>,<表2>..}
        [WHERE <表达式>]
        [GROUP BY <字段名>]
        [HAVING <表达式> [{<操作符1> <表达式1>}][..]]
        [ORDER BY <字段名> ASC | DESC ]
        [LIMIT <行数> [ OFFSET <偏移量> ]]
    ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

约定:

  • {} 内必选
  • [] 内可选
  • <> 内参数: 表达式、字段、参数
  • |  或者

二、单表查询

2.01 查询所有字段

语法:

select * from 表名;
  • 1

例:

select * from mytable;
  • 1

2.02 查询指定字段

语法:

select 字段名 from 表名;
  • 1

例:

select id,name,age from employee;
  • 1
  • 2

2.03 查询指定记录

语法:

select *|字段名 from 表名 where 字段=值;
  • 1

where支持的条件判断符:

操作符 说明
= 等于
<> , != 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
between A and B 在A与B之间

例1:

select * from employee where age>=18;
  • 1

例2:

select age,name from employee where salary between 2000 and 3000;
  • 1

2.04 范围查询--IN 的用法

语法:

select *|字段名 from 表名 where 字段 in (列表);
  • 1

例1: 本命年员工

select id,name from employee where age in (24,36,48,60);
  • 1

例2: 某列表中员工 (char varing[]类型不支持)

select * from employee where name in ('张三','李四','王五'); 
  • 1

2.05 范围查询--BETWEEN AND 的用法

语法:在值A到B之间(包括A,B)

select *|字段名 from 表名 where 字段 between A and B;
  • 1

例1:

select name from employee where age between 18 and 20;
  • 1

2.06 模糊查询--LIKE 的用法

语法:字段包含字符串strA的记录,一般搭配 % 匹配任意字符。

select *|字段名 from 表名 where 字段 like strA;
  • 1

例1:所有姓张的,(char varing[]类型不支持)

select * from employee where name like '张%';
  • 1

例2:所有山东人,(char varing[]类型不支持)

select * from employee where hometown like '%山东%';
  • 1

2.07 多条件查询--AND 、 OR 的用法

语法:and并且关系,or或者关系

select *|字段名 from 表名 where 字段1 条件1 and 字段2 条件2;
  • 1
select *|字段名 from 表名 where 字段1 条件1 or 字段2 条件2;
  • 1

例1: 四十岁以上(且)姓张的.

select * from employee where name like '张%' and age >=40;
  • 1

例2: 四十岁以上或者薪水过万的.

select * from employee where salary >=10000 or age >=40;
  • 1

2.07x 多表查询

语法:与条件联合使用,否则将造成 m*n 倍增.m,n为记录数.

select 表名1.字段1,别名2.字段2 from 表名1,表名2 [别名2] <条件>;
  • 1

例:

select u.name,t.name from user u,team t where t.id=u.team_id;
  • 1

2.08 空值查询 IS NULL

语法: IS NULL

select *|字段名 from 表名 where 字段1 条件1 and 字段2 条件2;
  • 1

例2: 邮箱为空的.

select * from employee where email is null;
  • 1

2.09 分组查询:--GROUP BY的用法

GROUP BY 查询分组情况 
/不会——————————-

语法: GROUP BY 字段1,按 字段1 分组。HAVING+条件 可以对分组进行筛选。

select *|字段名 from 表名 group by 字段;
  • 1

例1: 按性别分组.

select * from employee group by sexy;
  • 1

例1: .

select id,name,age from employee group by age having count(age)>=10;
  • 1

ERROR* 
column “mytable.ename” must appear in the GROUP BY clause or be used in an aggregate function 
——————————-不会/

添坑: 
事实上group by 会将select <字段1> 结果集中<字段1>相同的记录合并, 
所以,select <字段> 指定的字段必须 
1. 放入group by后,或者 
2. 放入having聚合函数中.

而且,group by 一般用来统计记录的分组情况,比如:分组,种类,部门. 
结合having 聚合

select esex,count(*) as total from employee group by esex order by esex DESC;
  • 1
esex total
F 19
M 23

2.10 查询结果:去重复--DISTINCT的用法

DISTINCT <字段> ,返回<字段>不同的记录

select distinct *|单字段名 from 表名;
  • 1

例1:

select distinct *|单字段名 from 表名;
  • 1

2.11 查询结果:排序--ORDER BY 的用法

ORDER BY 对查询结果排序 
语法: ASC升序、DESC降序。[ASC|DESC]

select *|字段名 from 表名 order by 字段1 ASC|DESC;
  • 1

例1:按年龄增排序;查询所有员工.

select * from employee order by age ASC;
  • 1

例2:与GROUP BY联用;按性别分组,按年龄排序.

select * from employee group by sex order by age ASC;
  • 1

2.12 查询结果:限制数量--LIMIT 的用法

语法: LIMIT A [OFFSET B]; 限制查询数量A个,偏移量B个.(从第B条往后A条数据)

select *|字段名 from 表名 limit A offset B;
  • 1

例1:每页100条数据,偏移量300。(相当于第4页)

select * from employee limit 100 offset 300;
  • 1

三、聚合函数查询

聚合函数

函数 描述
COUNT() 统计某列的行数,*则是表的字段数,指定列某记录为null时此条将被忽略
AVG() 统计某列的平均值,多列需要多次使用avg()
MAX() 求某列的最大值,支持日期和字符串比较
MIN() 求某列的最小值,同max()
SUM() 求某列的和,指定列某记录值为null时此条将被忽略

3.01 COUNT()函数

语法: [as str] 别名str (放到 str 列中)

select [<字段>,]count(字段) [as str] from 表名;
  • 1

例1:统计所有数据数量

select count(*) as total from employee;
  • 1

例2:按性别统计员工数量

select esex,count(*) as total from employee group by esex;
  • 1

3.02 SUM()函数

语法: [as str] 别名str (放到 str 列中)

select [<字段>,]sum(字段) [as str] from 表名;
  • 1

例1:统计薪水总和

select sum(salary) as total from employee;
  • 1

例2:统计不同性别的薪水情况

select sex,sum(salary) as total from employee group by sex;
  • 1

3.03 AVG()函数

语法: [as str] 别名str (放到 str 列中)

select [<字段>,]avg(字段) [as str] from 表名;
  • 1

例1:统计年龄平均值

select avg(age) as average from employee;
  • 1

例2:统计不同性别的年龄平均值

select sex,avg(age) as total from employee group by sex;
  • 1

3.04 MAX()函数

语法: [as str] 别名str (放到 str 列中)

select [<字段>,]max(字段) [as str] from 表名;
  • 1

例1:统计薪水最大值

select max(salary) as maximum from employee;
  • 1

例2:统计不同性别的薪水最大值

select sex,max(salary) as maximum from employee group by sex;
  • 1

3.05 MIN()函数

同MAX()的用法。

四、连接查询

4.01 内连接查询

  • 1

4.02 外连接查询

4.03 复合条件连接查询

五、子查询

5.01 ANY、SOME

满足任意一个即可,any some 等价 
where id > any (1,2,3) 
where age > some (select age from table2…)

5.02 ALL

必须满足所有 
where age > all (1,2.3)

5.03 EXISTS

满足exists子查询不为空 时进行外围查询。 
select * form table where exists (select c1 form table2)

5.04 IN

where id in (1,2,3);

where id in (select ..)

六、合并查询结果--UNION [ALL]

union 将几次查询结果合并到一起,(从后面)连接起来。这就要求几次查询的字段名(别名也可以)、数据类型全部一致。 
ALL将不合并重复字段。

select c1,c2 from table1
union
select c1,c2 from table2
  • 1
  • 2
  • 3
select c3,c4 from table3
union all
select c1,c2 from table2

猜你喜欢

转载自blog.csdn.net/hanyuyang19940104/article/details/80737616