- <pre name="code" class="sql">--登陆用户类型表
- 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,'数据管理员');</pre><pre name="code" class="sql">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的sequence
- create 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');</pre>insert into users values (sqe_user_id.nextval,2,'zhoukai56','asavakitm2');<br>
- <br>
- <pre></pre>
- <p><span style="font-size:16px">查询语句</span></p>
- <p>给表名起别名:</p>
- <p>select s.user_id from users s; --在做查询的时候表users被s代替</p>
- <p>字段连接符"||"</p>
- <p>select s.user_id||s.user_name b from users s; --讲字段user_id和user_name 连接起来 相当于java的"+"号 并且将连接的字段起名为B;</p>
- <p>消除重复数据:</p>
- <p>select distinct user_pwd from users; --重复的user_pwd只显示一次</p>
- <p><span style="font-size:24px"><strong>Where 条件:select *from users where 条件;</strong></span></p>
- <p>条件1:选取user id为1和2之间的(符号用法,和and 的用法):</p>
- <p></p>
- <pre name="code" class="sql">select *from users where users.user_id<2 and users.user_id>1;</pre>
- <p></p>
- <p>条件2:选取user_id为1和3(包含1,3)之间(between and 的使用)</p>
- <p></p>
- <pre name="code" class="sql">select *from users where users.user_id between 1 and 2;</pre>
- <p></p>
- <p>条件3:选取user_id为1或者3的(or的使用)</p>
- <p></p>
- <pre name="code" class="sql">select *from users where users.user_id=1 or users.user_id=2;</pre>
- <p></p>
- <p>条件4:选取用户名为 zhoukai和zhoukaigaga的(in 的使用)</p>
- <p></p>
- <pre name="code" class="sql">select *from users where users.user_name in('zhoukai','zhoukaigaga');</pre>条件5:选取用户名带有"ga" 字串的用户名(like的用法)
- <p></p>
- <p></p>
- <pre name="code" class="sql"><span style="font-size:10px;">select *from users where users.user_name like '%ga%';--<span style="color: rgb(128, 0, 0); ">%零或者多个字符 _单一任何字符(下划线) \特殊字符 []在某一范围内的字符</span></span></pre><pre name="code" class="sql"><span style="font-size:10px;"><span style="color: rgb(128, 0, 0); ">如[0-9]或者[aeth] [^]不在某范围内的字符,如[^0-9]或者[^aeth][^a-z]</span></span></pre>如果想将%号作为普通字符可以使用escape转义为一般字符<br>
- <pre name="code" class="sql">select *from users where users.user_name like '%ga/%' escape '/'; --这时候搜索的就是 %ga+'%' 这样的了</pre>
- <p></p>
- <p>条件6:选取用户为空的字段(假设,有约束在不可能为空)(is null 的使用)</p>
- <p></p>
- <pre name="code" class="sql">select *from users where user_name is null;</pre>条件7:not的使用 不在...没有... 不是...
- <p></p>
- <p></p>
- <pre name="code" class="sql">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和25
- select * from users where users_name is not null; --用户id不为空</pre>
- <p></p>
- <p>以上的条件都可以结合起来用比如:</p>
- <p></p>
- <pre name="code" class="sql">select *from users where user_id >1 and user_id not in (2); --用户id大于2 但是不是2</pre>
- <p></p>
- <p><span style="font-size:24px">表的排序:(order by) 在where条件之后使用</span></p>
- <p><span style="font-size:16px"></span></p>
- <pre name="code" class="sql">select *from users order by user_id esc; --升序,可以不使用esc默为esc<pre name="code" class="sql">select *from users order by user_id desc; --降序</pre>
- <pre></pre>
- 函数:参见 <a href="http://blog.youkuaiyun.com/joker_zhou/article/details/7378287">点击打开链接</a>
- <p></p>
- <p><span style="font-size:24px"><strong>多表查询:</strong></span></p>
- <p><span style="font-size:24px"><strong>表连接:</strong></span></p>
- <p><span style="font-size:10px">内连接:</span></p>
- <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>
- <pre name="code" class="sql" style="font-size: 14px; ">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联合起来成为一张表 ,只有两个表都存在的时候才连接</pre><span style="font-family:arial,宋体,sans-serif; line-height:24px"><span style="font-size:10px">外连接:<br>
- </span></span>
- <p><span style="font-family:arial,宋体,sans-serif; font-size:10px"><span style="line-height:24px"><span style="font-family:arial,宋体,sans-serif; line-height:24px">左连接</span></span></span></p>
- <p><span style="font-family:arial,宋体,sans-serif; font-size:10px"><span style="line-height:24px"><span style="font-family:arial,宋体,sans-serif; line-height:24px"></span></span></span></p>
- <pre name="code" class="sql">select *from users left join user_type on users.user_type_id=user_type.type_id; --使用条件将两个表联合起来,完整显示左表(users表) ,对于没有关联的右表则不显示, user_type.type_id为3的就没有显示</pre>右连接<br>
- <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>
- <pre name="code" class="sql" style="font-size: 14px; ">select *from users right join user_type on users.user_id=user_type.type_id; --如同左连接,这次右表(user_type表)为主表,users表为显示完整</pre><span style="font-size:10px">全连接</span><br>
- <pre name="code" class="sql">select *from users full join user_type on users.user_type_id=user_type.type_id; --左表右表都显示完整</pre>自连接<br>
- <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>
- <pre name="code" class="sql">select *from users s,users u where s.user_id=u.user_id; </pre>交叉连接(做笛卡尔积 返回表1*表2 个数的表)
- <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>
- <pre name="code" class="sql">select * from users, user_type; </pre><pre name="code" class="sql">select * from users cross join user_type;</pre>等值连接<br>
- <pre name="code" class="sql">select * from users, user_type where users.user_id=2; --就是 笛卡尔积 加上条件</pre>
- <p><span style="font-size:24px"><strong>分组:</strong></span></p>
- <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>
- <pre name="code" class="sql">select 只能出现分组的字段/或者聚合函数 from users order by 分组字段 having 分组条件(只能出现分组字段); ps:分组中不能使用wher 所有条件都是having</pre><pre name="code" class="sql"></pre><pre name="code" class="sql"></pre>
- <p>select 后的聚合函数计算的是"函数内字段"的值.例如:</p>
- <p></p>
- <pre name="code" class="sql">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</pre>
- <p></p>
- <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"><span style="font-size:24px"><strong>子查询:</strong></span></span></span></p>
- <p><span style="font-family:arial,宋体,sans-serif; font-size:10px"><span style="line-height:24px">子查询就是讲查询出来的数据作为表来使用</span></span></p>
- <p><span style="font-family:arial,宋体,sans-serif; font-size:10px"><span style="line-height:24px"><span style="font-family:arial,宋体,sans-serif; line-height:24px; white-space:pre; background-color:rgb(240,240,240); font-size:24px"><strong>查询数据库字典(就是查询多少个表之类的):</strong></span><br>
- </span></span></p>
- <p><span style="font-family:arial,宋体,sans-serif; font-size:10px"><span style="line-height:24px"><span style="font-family:arial,宋体,sans-serif; line-height:24px; white-space:pre; background-color:rgb(240,240,240)"><strong><br>
- </strong></span></span></span></p>
- <p><br>
- </p>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
Oracle各种查询
最新推荐文章于 2024-03-15 11:36:30 发布