**
声明:
**
employee表:
eid [PK] | ename | eage | esalary | esex | eemail |
---|---|---|---|---|---|
integer | character varying(20) | integer | integer | character varying(1) | character varying(32) |
SELECT
{* | <字段列表>}
[
{FROM <表1>,<表2>..}
[WHERE <表达式>]
[GROUP BY <字段名>]
[HAVING <表达式> [{<操作符1> <表达式1>}][..]]
[ORDER BY <字段名> ASC | DESC ]
[LIMIT <行数> [ OFFSET <偏移量> ]]
]
约定:
语法:
select * from 表名;
例:
select * from mytable;
语法:
select 字段名 from 表名;
例:
select id,name,age from employee;
语法:
select *|字段名 from 表名 where 字段=值;
where支持的条件判断符:
操作符 | 说明 |
---|---|
= | 等于 |
<> , != | 不等于 |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
between A and B | 在A与B之间 |
例1:
select * from employee where age>=18;
例2:
select age,name from employee where salary between 2000 and 3000;
语法:
select *|字段名 from 表名 where 字段 in (列表);
例1: 本命年员工
select id,name from employee where age in (24,36,48,60);
例2: 某列表中员工 (char varing[]类型不支持)
select * from employee where name in ('张三','李四','王五');
语法:在值A到B之间(包括A,B)
select *|字段名 from 表名 where 字段 between A and B;
例1:
select name from employee where age between 18 and 20;
语法:字段包含字符串strA的记录,一般搭配 % 匹配任意字符。
select *|字段名 from 表名 where 字段 like strA;
例1:所有姓张的,(char varing[]类型不支持)
select * from employee where name like '张%';
例2:所有山东人,(char varing[]类型不支持)
select * from employee where hometown like '%山东%';
语法:and并且关系,or或者关系
select *|字段名 from 表名 where 字段1 条件1 and 字段2 条件2;
select *|字段名 from 表名 where 字段1 条件1 or 字段2 条件2;
例1: 四十岁以上(且)姓张的.
select * from employee where name like '张%' and age >=40;
例2: 四十岁以上或者薪水过万的.
select * from employee where salary >=10000 or age >=40;
语法:与条件联合使用,否则将造成 m*n 倍增.m,n为记录数.
select 表名1.字段1,别名2.字段2 from 表名1,表名2 [别名2] <条件>;
例:
select u.name,t.name from user u,team t where t.id=u.team_id;
语法: IS NULL
select *|字段名 from 表名 where 字段1 条件1 and 字段2 条件2;
例2: 邮箱为空的.
select * from employee where email is null;
GROUP BY 查询分组情况
/不会——————————-
语法: GROUP BY 字段1,按 字段1 分组。HAVING+条件 可以对分组进行筛选。
select *|字段名 from 表名 group by 字段;
例1: 按性别分组.
select * from employee group by sexy;
例1: .
select id,name,age from employee group by age having count(age)>=10;
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;
esex | total |
---|---|
F | 19 |
M | 23 |
DISTINCT <字段> ,返回<字段>不同的记录
select distinct *|单字段名 from 表名;
例1:
select distinct *|单字段名 from 表名;
ORDER BY 对查询结果排序
语法: ASC升序、DESC降序。[ASC|DESC]
select *|字段名 from 表名 order by 字段1 ASC|DESC;
例1:按年龄增排序;查询所有员工.
select * from employee order by age ASC;
例2:与GROUP BY联用;按性别分组,按年龄排序.
select * from employee group by sex order by age ASC;
语法: LIMIT A [OFFSET B]; 限制查询数量A个,偏移量B个.(从第B条往后A条数据)
select *|字段名 from 表名 limit A offset B;
例1:每页100条数据,偏移量300。(相当于第4页)
select * from employee limit 100 offset 300;
聚合函数
函数 | 描述 |
---|---|
COUNT() | 统计某列的行数,*则是表的字段数,指定列某记录为null时此条将被忽略 |
AVG() | 统计某列的平均值,多列需要多次使用avg() |
MAX() | 求某列的最大值,支持日期和字符串比较 |
MIN() | 求某列的最小值,同max() |
SUM() | 求某列的和,指定列某记录值为null时此条将被忽略 |
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]count(字段) [as str] from 表名;
例1:统计所有数据数量
select count(*) as total from employee;
例2:按性别统计员工数量
select esex,count(*) as total from employee group by esex;
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]sum(字段) [as str] from 表名;
例1:统计薪水总和
select sum(salary) as total from employee;
例2:统计不同性别的薪水情况
select sex,sum(salary) as total from employee group by sex;
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]avg(字段) [as str] from 表名;
例1:统计年龄平均值
select avg(age) as average from employee;
例2:统计不同性别的年龄平均值
select sex,avg(age) as total from employee group by sex;
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]max(字段) [as str] from 表名;
例1:统计薪水最大值
select max(salary) as maximum from employee;
例2:统计不同性别的薪水最大值
select sex,max(salary) as maximum from employee group by sex;
同MAX()的用法。
满足任意一个即可,any some 等价
where id > any (1,2,3)
where age > some (select age from table2…)
必须满足所有
where age > all (1,2.3)
满足exists子查询不为空 时进行外围查询。
select * form table where exists (select c1 form table2)
where id in (1,2,3);
where id in (select ..)
union 将几次查询结果合并到一起,(从后面)连接起来。这就要求几次查询的字段名(别名也可以)、数据类型全部一致。
ALL将不合并重复字段。
select c1,c2 from table1
union
select c1,c2 from table2
select c3,c4 from table3
union all
select c1,c2 from table2