oracle——数据表的数据查询——distinct 关键字、group by 字句、having子句

SQL查询

create table yuangong ( id number,
                        name varchar2(50),
                        zhiwei varchar2(50),
                        age number,
                        sex varchar2(20)
                      ) tablespace test
                      
insert into yuangong values(1,'周磊01','操作工',21,'男');
insert into yuangong values(2,'周磊02','操作工',23,'女');
insert into yuangong values(3,'周磊03','操作工',22,'男');
insert into yuangong values(4,'周磊04','操作工',24,'女');
insert into yuangong values(5,'周磊05','操作工',26,'男');
insert into yuangong values(6,'周磊06','操作工',25,'女');
insert into yuangong values(7,'周磊07','操作工',29,'男');
insert into yuangong values(8,'周磊08','操作工',28,'女');
insert into yuangong values(9,'周磊09','操作工',27,'男');
insert into yuangong values(10,'周磊10','操作工',25,'女');

insert into yuangong values(11,'周11磊','头目',39,'男');
insert into yuangong values(12,'周12磊','头目',31,'女');
insert into yuangong values(13,'周13磊','头目',37,'男');
insert into yuangong values(14,'周14磊','头目',34,'女');

insert into yuangong values(15,'周15磊','经理',45,'男');


select * from yuangong;
------------------------------------------------------------------

create table xinzi ( id number,
                     yuangonghao number,
                     yuefen number,
                     jine number
                    ) tablespace test
                    

select * from xinzi;

insert into xinzi values(1,1,1,3800);
insert into xinzi values(2,1,2,3600);
insert into xinzi values(3,1,3,3300);
insert into xinzi values(4,1,4,3200);


insert into xinzi values(5,2,1,2800);
insert into xinzi values(6,2,2,2600);
insert into xinzi values(7,2,3,2300);
insert into xinzi values(8,2,4,2200);

insert into xinzi values(9,3,1,2200);
insert into xinzi values(10,3,2,2500);
insert into xinzi values(11,3,3,2300);
insert into xinzi values(12,3,4,2800);

insert into xinzi values(13,4,1,1200);
insert into xinzi values(14,4,2,1500);
insert into xinzi values(15,4,3,1300);
insert into xinzi values(16,4,4,1800);

---------------------------------------------------

insert into xinzi values(17,11,1,4200);
insert into xinzi values(18,11,2,4500);
insert into xinzi values(19,11,3,4300);
insert into xinzi values(20,11,4,4800);

insert into xinzi values(21,12,1,4800);
insert into xinzi values(22,12,2,4600);
insert into xinzi values(23,12,3,4300);
insert into xinzi values(24,12,4,4200);

insert into xinzi values(25,13,1,4200);
insert into xinzi values(26,13,2,4300);
insert into xinzi values(27,13,3,4400);
insert into xinzi values(28,13,4,4500);

insert into xinzi values(29,13,1,4200);
insert into xinzi values(30,13,2,4300);
insert into xinzi values(31,13,3,4400);
insert into xinzi values(32,13,4,4500);
-------------------------------------------------------

insert into xinzi values(33,15,1,7300);
insert into xinzi values(34,15,2,7700);
insert into xinzi values(35,15,3,7500);
insert into xinzi values(36,15,4,7900);


------------------------------------------------------------------------------------



distinct 关键字可以获得唯一性记录,被distinct限制的既可以是单个列,也可以是多个列的组合。

select yuefen from xinzi;

select distinct yuefen from xinzi;


----------------------------------------------------------------------------------------------------

order by 关键字可以将查询的结果,按照一定的顺序进行排序,asc 升序;desc 降序;

select jine from xinzi order  by  jine desc;

select jine from xinzi order  by  jine asc;
--------------------------------------------------------------------------------------------------------

group by 字句

group by字句用于对记录集合进行分组。一旦使用了分组之后,select语句的真实操作目标即为各个分组数据,每次想循环处理的也是各个分组,

而不再是单条记录。


统计每一位员工的工资

select name,sum(jine) from yuangong,xinzi where yuangong.id = xinzi.yuangonghao 
group by  yuangong.id,name;


注意:order by子句是对最终结果进行排序的,因此,如果一条查询语句中同时存在着group by和order by子句,那么应该将order by 子句置于

group by之后,并且order by子句的排序标准不能出现group by子句定义之外的列。

------------------------------------------------------------------------------------------------------------------------------------

having子句

where子句会对from子句所定义的数据源进行条件过滤,但是针对group by子句形成的分组之后的结果集,where子句将会无能为力。

为了过滤group by子句产生的结果集,可以使用having子句


select name,sum(jine) from yuangong,xinzi where yuangong.id = xinzi.yuangonghao 
group by yuangong.id,yuangong.name 
having sum(jine) > 8500 
order by sum(jine); 


======================================================================================================================
drop table yuangong;
drop table xinzi;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值