PostgreSQL数据查询_postgresql查询表的字段数

employee表:

eid [PK]enameeageesalaryesexeemail
integercharacter varying(20)integerintegercharacter 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
esextotal
F19
M23
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 列中)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值