oralce 取前几条数据 分页查询 左右内外连接 order by group by

取前几条数据

MSSQL

如在ms   sqlserver   用此语句:  
  select   top   2   *   from   test01   就会只显示前2条记录,  

MYSQL

select   *   from   your_table     limit   2   
    
  使用limit就可以了.

Oracle的语句: 

select * from (select rownum r ,* from test) tt 
where tt.r > 50 and tt.r <= 100;  (最好用PLSQL游标来解决)


DB2中: 
select * from payment fetch first 5 row only --查前5条记录 

oralce 分页查询

从第11条到第20条

select * from (

       select row_.*, rownum rownum_ from (
                          select person_id, chn_name, chn_firstname_py from t_pbase_info
                          ) row_ where rownum <=20
               ) where rownum_ >=11


oralce 左右内全连接

Sql代码   收藏代码
  1. --建立测试数据  
  2. create table a(id number);  
  3. create table b(id number);  
  4. insert into a values(1);  
  5. insert into a values(2);  
  6. insert into a values(3);  
  7. insert into b values(1);  
  8. insert into b values(2);  
  9. insert into b values(4);  
  10. commit;  
  11.   
  12. --左:  
  13. --主流数据库通用的方法  
  14. select * from a left join b on a.id=b.id;  
  15. --Oracle特有的方法  
  16. select * from a, b where a.id=b.id(+);  
  17.   
  18.         ID         ID  
  19. ---------- ----------  
  20.          1          1  
  21.          2          2  
  22.          3   
  23.   
  24.   
  25. --右:  
  26. --主流数据库通用的方法  
  27. select * from a right join b on a.id=b.id;  
  28. --Oracle特有的方法  
  29. select * from a, b where a.id(+)=b.id;  
  30.   
  31.         ID         ID  
  32. ---------- ----------  
  33.          1          1  
  34.          2          2  
  35.                     4  
  36.            
  37.            
  38. --内  
  39. --主流数据库通用的方法  
  40. select * from a join b on a.id=b.id;  
  41. --where关联  
  42. select * from a, b where a.id=b.id;  
  43.   
  44.         ID         ID  
  45. ---------- ----------  
  46.          1          1  
  47.          2          2  
  48.            
  49.            
  50. --全外  
  51. --主流数据库通用的方法  
  52. select * from a full join b on a.id=b.id;  
  53. --Oracle特有的方法  
  54. select *  
  55.   from a, b  
  56.  where a.id = b.id(+)  
  57. union  
  58. select *   
  59.   from a, b   
  60.  where a.id(+) = b.id;  
  61.   
  62.         ID         ID  
  63. ---------- ----------  
  64.          1          1  
  65.          2          2  
  66.          3   
  67.                     4  
  68.   
  69.   
  70. --完全,也叫交叉连接或者笛卡尔积  
  71. --主流数据库通用的方法  
  72. select * from a,b;  
  73. --或者  
  74. select * from a cross join b;  
  75.   
  76.         ID         ID  
  77. ---------- ----------  
  78.          1          1  
  79.          1          2  
  80.          1          4  
  81.          2          1  
  82.          2          2  
  83.          2          4  
  84.          3          1  
  85.          3          2  
  86.          3          4  
  87.   
  88.   
  89. 连接无非是这几个  
  90. --内连接和where相同  
  91. inner join  
  92. --左向外连接,返回左边表所有符合条件的  
  93. left join  
  94. --右向外连接,返回右边表所有符合条件的  
  95. right join  
  96. --完整外部连接,左向外连接和右向外连接的合集  
  97. full join  
  98. --交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合  
  99. cross join  
  100.   
  101.   
  102. --补充:  
  103. --左向外连接,返回左边表所有符合条件的,  
  104. --注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录  
  105. select *   
  106.   from a, b  
  107.  where a.id = b.id(+)  
  108.    and b.id = 2;  
  109.      
  110.         ID         ID  
  111. ---------- ----------  
  112.          2          2     
  113.            
  114.            
  115. --左向外连接,返回左边表所有符合条件的  
  116. --注意where上第二个加号,它的作用是修改右边表记录的显示,例如如果b.id(+) = 2,显示为2,否则显示null  
  117. select *  
  118.   from a, b  
  119.  where a.id = b.id(+)  
  120.    and b.id(+) = 2;  
  121.   
  122.         ID         ID  
  123. ---------- ----------  
  124.          2          2  
  125.          3   
  126.          1      

  127. order by  group by 区别
  128. order by 排序查询、asc升序、desc降序
    示例:
    select * from 学生表 order by 年龄    查询学生表信息、按年龄的升序(默认、可缺省、从低到高)排列显示
    也可以多条件排序、 比如 order by 年龄,成绩 desc     按年龄升序排列后、再按成绩降序排列
     
    group by  分组查询、having 只能用于group by子句、作用于组内,having条件子句可以直接跟函数表达式。使用group by 子句的查询语句需要使用聚合函数。
    示例:
    select 学号,SUM(成绩) from 选课表 group by 学号  按学号分组、查询每个学号的总成绩
     
    select 学号,AVG(成绩) from 选课表  
    group by 学号
    having AVG(成绩)>(select AVG(成绩) from 选课表 where 课程号='001')
    order by AVG(成绩) desc
    查询平均成绩大于001课程平均成绩的学号、并按平均成绩的降序排列
               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值