Oracle各种查询

  1. <pre name="code" class="sql">--登陆用户类型表  
  2. create table user_type(  
  3. type_id number(32),  
  4. type_name varchar(20)  
  5. )  
  6. alter table user_type add constraint primary key(type_id);  
  7. insert into user_type values (1,'普通用户');  
  8. insert into user_type values (2,'数据管理员');</pre><pre name="code" class="sql">insert into user_type values (3,null); --测试用  
  9. --登陆用户表  
  10. create table users(  
  11. user_id number(32),  
  12. user_type_id number(32),  
  13. user_name varchar(32),  
  14. user_pwd varchar(32)  
  15. );  
  16. alter table users add constraint pk_usersid primary key(user_id);  
  17. alter table users add constraints  fk_user_typeid foreign key(user_type_id) references user_type(type_id);  
  18. alter table users modify user_name unique;  
  19. alter table users modify user_name not null;  
  20. alter table users modify user_pwd not null;  
  21. --建立users的sequence  
  22. create sequence sqe_user_id;  
  23. insert into users values(sqe_user_id.nextval,1,'zhoukai','asavakitm2');  
  24. insert into users values(sqe_user_id.nextval,2,'zhoukaidba','asavakitm2');  
  25. insert into users values(sqe_user_id.nextval,2,'zhoukaigaga','asavakitm2');  
  26. insert into users values(sqe_user_id.nextval,1,'zhoukaidba2','asavakitm2');</pre>insert into users values (sqe_user_id.nextval,2,'zhoukai56','asavakitm2');<br>  
  27. <br>  
  28. <pre></pre>  
  29. <p><span style="font-size:16px">查询语句</span></p>  
  30. <p>给表名起别名:</p>  
  31. <p>select s.user_id from users s; --在做查询的时候表users被s代替</p>  
  32. <p>字段连接符"||"</p>  
  33. <p>select s.user_id||s.user_name b from users s; --讲字段user_id和user_name 连接起来 相当于java的"+"号 并且将连接的字段起名为B;</p>  
  34. <p>消除重复数据:</p>  
  35. <p>select distinct user_pwd from users; --重复的user_pwd只显示一次</p>  
  36. <p><span style="font-size:24px"><strong>Where 条件:select *from users where  条件;</strong></span></p>  
  37. <p>条件1:选取user id为1和2之间的(符号用法,和and 的用法):</p>  
  38. <p></p>  
  39. <pre name="code" class="sql">select *from users where users.user_id<2 and users.user_id>1;</pre>  
  40. <p></p>  
  41. <p>条件2:选取user_id为1和3(包含1,3)之间(between and 的使用)</p>  
  42. <p></p>  
  43. <pre name="code" class="sql">select *from users where users.user_id between 1 and 2;</pre>  
  44. <p></p>  
  45. <p>条件3:选取user_id为1或者3的(or的使用)</p>  
  46. <p></p>  
  47. <pre name="code" class="sql">select *from users where users.user_id=1 or users.user_id=2;</pre>  
  48. <p></p>  
  49. <p>条件4:选取用户名为 zhoukai和zhoukaigaga的(in 的使用)</p>  
  50. <p></p>  
  51. <pre name="code" class="sql">select *from users where users.user_name in('zhoukai','zhoukaigaga');</pre>条件5:选取用户名带有"ga" 字串的用户名(like的用法)  
  52. <p></p>  
  53. <p></p>  
  54. <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>  
  55. <pre name="code" class="sql">select *from users where users.user_name like '%ga/%' escape '/'--这时候搜索的就是  %ga+'%' 这样的了</pre>  
  56. <p></p>  
  57. <p>条件6:选取用户为空的字段(假设,有约束在不可能为空)(is null 的使用)</p>  
  58. <p></p>  
  59. <pre name="code" class="sql">select *from users where user_name is null;</pre>条件7:not的使用 不在...没有... 不是...  
  60. <p></p>  
  61. <p></p>  
  62. <pre name="code" class="sql">select * from users where users_id not between 22 and 26; --用户id不在22 到 26之间(包含22和26)  
  63. select * from users where users_id not in(22,25); --用户id 不在22和25  
  64. select * from users where users_name is not null;  --用户id不为空</pre>  
  65. <p></p>  
  66. <p>以上的条件都可以结合起来用比如:</p>  
  67. <p></p>  
  68. <pre name="code" class="sql">select *from users where user_id >1 and user_id not in (2); --用户id大于2 但是不是2</pre>  
  69. <p></p>  
  70. <p><span style="font-size:24px">表的排序:(order by) 在where条件之后使用</span></p>  
  71. <p><span style="font-size:16px"></span></p>  
  72. <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>  
  73. <pre></pre>  
  74. 函数:参见 <a href="http://blog.youkuaiyun.com/joker_zhou/article/details/7378287">点击打开链接</a>  
  75. <p></p>  
  76. <p><span style="font-size:24px"><strong>多表查询:</strong></span></p>  
  77. <p><span style="font-size:24px"><strong>表连接:</strong></span></p>  
  78. <p><span style="font-size:10px">内连接:</span></p>  
  79. <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>  
  80. <pre name="code" class="sql" style="font-size: 14px; ">select * from users s [innerjoin 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>  
  81. </span></span>  
  82. <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>  
  83. <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>  
  84. <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>  
  85. <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>  
  86. <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>  
  87. <pre name="code" class="sql">select *from users full join user_type on users.user_type_id=user_type.type_id; --左表右表都显示完整</pre>自连接<br>  
  88. <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>  
  89. <pre name="code" class="sql">select *from users s,users u where s.user_id=u.user_id;  </pre>交叉连接(做笛卡尔积 返回表1*表2 个数的表)  
  90. <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>  
  91. <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>  
  92. <pre name="code" class="sql">select * from users, user_type where users.user_id=2; --就是 笛卡尔积 加上条件</pre>  
  93. <p><span style="font-size:24px"><strong>分组:</strong></span></p>  
  94. <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"></span></span></p>  
  95. <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>  
  96. <p>select 后的聚合函数计算的是"函数内字段"的值.例如:</p>  
  97. <p></p>  
  98. <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>  
  99. <p></p>  
  100. <p><span style="font-family:arial,宋体,sans-serif"><span style="line-height:24px"><span style="font-size:24px"><strong>子查询:</strong></span></span></span></p>  
  101. <p><span style="font-family:arial,宋体,sans-serif; font-size:10px"><span style="line-height:24px">子查询就是讲查询出来的数据作为表来使用</span></span></p>  
  102. <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>  
  103. </span></span></p>  
  104. <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>  
  105. </strong></span></span></span></p>  
  106. <p><br>  
  107. </p>  
  108. <pre></pre>  
  109. <pre></pre>  
  110. <pre></pre>  
  111. <pre></pre>  
  112. <pre></pre>  
  113. <pre></pre>  
  114.   
  115. </pre>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值