20181114教学sql

--精确查找:查询水表编号为30408的业主记录
SELECT * FROM T_OWNERS WHERE WATERMETER = '30408'
--模糊查询:查询业主名称包含'刘'的业主记录
SELECT * FROM T_OWNERS WHERE NAME LIKE '%刘%'
--AND运算符:查询业主名称包含'刘'的并且门牌号包含5的业主记录
SELECT * FROM T_OWNERS WHERE NAME LIKE '%刘%' AND HOUSENUMBER LIKE '%5%'
--or运算符:查询业主名称包含'刘'的或者门牌号包含5的业主记录
select * from t_owners where name like '%刘%' or housenumber like '%5%'
--and与or混合使用:查询业主名称包含'刘'的或者门牌号包含5的业主记录,并且地址编号为3的记录
select * from t_owners where (name like '%刘%' or housenumber like '%5%') and addressid = 3
--范围查询:查询台账记录中用水字数大于等于10000并且小于等于20000的记录
select * from t_account where usenum>=10000 and usenum<=20000
select * from t_account where usenum between 10000 and 20000
--空值查询:查询t_pricetable 表中maxnum为空的记录
select * from t_pricetable where maxnum is null
--不为空
select * from t_pricetable where maxnum is not null
--去重:查询业主表中的地址ID 不重复显示
select distinct addressid from t_owners

--聚合统计
--求和sum:统计2012年所有用户的用水量总和
select sum(usenum) from t_account where year = '2012'
--求平均数avg:统计2012年所有用水量的平均值
select avg(usenum) from t_account where year = '2012'
--求最大值max:统计2012年最高用水量
select max(usenum) from t_account where year = '2012'
select min(usenum) from t_account where year = '2012'
--统计记录数count:统计业主类型ID为1的业主记录
select count(*) from t_owners where ownertypeid = 1
--分组聚合:按区域分组统计水费合计数
select areaid,sum(money) from t_account group by areaid
--分组后条件查询having:查询水费合计大于16900的区域及水费合计
select areaid,sum(money) from t_account group by areaid having sum(money)>16900

--集合运算
--并集(包含重复记录)
select * from t_owners where id>5
union all
select * from t_owners where id<8
select * from t_owners where id>5
union
select * from t_owners where id<8
--交集
select * from t_owners where id>5
intersect
select * from t_owners where id<8
--差集
select * from t_owners where id>5
minus
select * from t_owners where id<8
select * from t_owners where id<8
minus
select * from t_owners where id>5

--序列
--创建简单序列
create sequence seq_test1;
--查询序列的下一个值
select seq_test1.nextval from dual;
--查询序列的当前值
select seq_test1.currval from dual;
--创建一个有最大值的非循环序列
create sequence seq_test2 maxvalue 5;
select seq_test2.nextval from dual;
--自定义增长值
create sequence seq_test3
increment by 10
start with 10
maxvalue 100
select seq_test3.nextval from dual;
--有最小值的序列
create sequence seq_test4
increment by 10
start with 10
minvalue 5
maxvalue 100
select seq_test4.nextval from dual;
--循环序列
create sequence seq_test5
increment by 10
start with 10
minvalue 10
maxvalue 210
cycle
select seq_test5.nextval from dual;

--同义词
--创建私有同义词
create synonym owner for t_owners;
select * from owner
select * from wateruser.t_owners
--创建公有同义词
create public synonym owner2 for t_owners;
select * from owner2
原文地址:https://www.cnblogs.com/niwotaxuexiba/p/9960468.html