Oracle各种查询

来源:互联网 发布:如何在淘宝上卖话费 编辑:程序博客网 时间:2024/05/29 04:18
--登陆用户类型表create table user_type(type_id number(32),type_name varchar(20))alter table user_type add constraint primary key(type_id);insert into user_type values (1,'普通用户');insert into user_type values (2,'数据管理员');
insert into user_type values (3,null); --测试用--登陆用户表create table users(user_id number(32),user_type_id number(32),user_name varchar(32),user_pwd varchar(32));alter table users add constraint pk_usersid primary key(user_id);alter table users add constraints  fk_user_typeid foreign key(user_type_id) references user_type(type_id);alter table users modify user_name unique;alter table users modify user_name not null;alter table users modify user_pwd not null;--建立users的sequencecreate sequence sqe_user_id;insert into users values(sqe_user_id.nextval,1,'zhoukai','asavakitm2');insert into users values(sqe_user_id.nextval,2,'zhoukaidba','asavakitm2');insert into users values(sqe_user_id.nextval,2,'zhoukaigaga','asavakitm2');insert into users values(sqe_user_id.nextval,1,'zhoukaidba2','asavakitm2');
insert into users values (sqe_user_id.nextval,2,'zhoukai56','asavakitm2');

查询语句

给表名起别名:

select s.user_id from users s; --在做查询的时候表users被s代替

字段连接符"||"

select s.user_id||s.user_name b from users s; --讲字段user_id和user_name 连接起来 相当于java的"+"号 并且将连接的字段起名为B;

消除重复数据:

select distinct user_pwd from users; --重复的user_pwd只显示一次

Where 条件:select *from users where  条件;

条件1:选取user id为1和2之间的(符号用法,和and 的用法):

select *from users where users.user_id<2 and users.user_id>1;

条件2:选取user_id为1和3(包含1,3)之间(between and 的使用)

select *from users where users.user_id between 1 and 2;

条件3:选取user_id为1或者3的(or的使用)

select *from users where users.user_id=1 or users.user_id=2;

条件4:选取用户名为 zhoukai和zhoukaigaga的(in 的使用)

select *from users where users.user_name in('zhoukai','zhoukaigaga');
条件5:选取用户名带有"ga" 字串的用户名(like的用法)

select *from users where users.user_name like '%ga%';--%零或者多个字符  _单一任何字符(下划线) \特殊字符 []在某一范围内的字符
如[0-9]或者[aeth] [^]不在某范围内的字符,如[^0-9]或者[^aeth][^a-z]
如果想将%号作为普通字符可以使用escape转义为一般字符
select *from users where users.user_name like '%ga/%' escape '/'; --这时候搜索的就是  %ga+'%' 这样的了

条件6:选取用户为空的字段(假设,有约束在不可能为空)(is null 的使用)

select *from users where user_name is null;
条件7:not的使用 不在...没有... 不是...

select * from users where users_id not between 22 and 26; --用户id不在22 到 26之间(包含22和26)select * from users where users_id not in(22,25); --用户id 不在22和25select * from users where users_name is not null;  --用户id不为空

以上的条件都可以结合起来用比如:

select *from users where user_id >1 and user_id not in (2); --用户id大于2 但是不是2

表的排序:(order by) 在where条件之后使用

select *from users order by user_id esc; --升序,可以不使用esc默为esc
select *from users order by user_id desc; --降序
函数:参见 点击打开链接

多表查询:

表连接:

内连接:

select * from users s [inner] join user_type u on s.user_type_id=u.type_id;--使用条件users.user_type_id和user_type.type_id联合起来成为一张表 ,只有两个表都存在的时候才连接
外连接:

左连接

select *from users left join user_type on users.user_type_id=user_type.type_id; --使用条件将两个表联合起来,完整显示左表(users表) ,对于没有关联的右表则不显示, user_type.type_id为3的就没有显示
右连接

select *from users right join user_type on users.user_id=user_type.type_id; --如同左连接,这次右表(user_type表)为主表,users表为显示完整
全连接
select *from users full join user_type on users.user_type_id=user_type.type_id; --左表右表都显示完整
自连接

select *from users s,users u where s.user_id=u.user_id;  
交叉连接(做笛卡尔积 返回表1*表2 个数的表)

select * from users, user_type; 
select * from users cross join user_type;
等值连接
select * from users, user_type where users.user_id=2; --就是 笛卡尔积 加上条件

分组:

select 只能出现分组的字段/或者聚合函数 from users order by 分组字段 having 分组条件(只能出现分组字段); ps:分组中不能使用wher 所有条件都是having

select 后的聚合函数计算的是"函数内字段"的值.例如:

select s.user_type_id,s.user_pwd,count(user_pwd) from users s group by s.user_type_id,s.user_pwd; count()函数的值分别为3和2

子查询:

子查询就是讲查询出来的数据作为表来使用

查询数据库字典(就是查询多少个表之类的):



	
				
		
原创粉丝点击