码迷,mamicode.com
首页 > 数据库 > 详细

重要的sql语句

时间:2021-02-09 12:24:17      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:sql   creat   rom   如何快速   如何   int   查询   let   事物   

 1:oracle 分页
 
         select * from (select t.*, rownum rn from (select * from menu order by id desc) t where  rownum < 10) where rn >=5
 
  2: mysql 分页
 
         select * from music where id limit 5,5
 
  
1、.不存在另一张表时:
create  table  新表  as  select * from 将要复制的表
2、存在另一张表时:
insert  into  新表名  select  字段  from  将要复制的表名
 
 3:oracle中如何快速将一张表的数据复制到另外一张表中(另外一张表不存在,另外一张       表存在,但数据为空)
 
  4:音乐专辑
 
   查询出special 表中的id  专辑名 并下面有多少首歌曲
 
      Select  s.id , min(s.sname),count(m.mid)  from  special s  inner
      join  ms  m  on  s.id=m.id  group  by  s.id
 
  5:快速删除一张表(不可事物回滚,也就是没有日志记录)
 
      TRUNCATE from 表名
 
  6:inner join
 
  select 查找信息 from 表名 1 inner join 表名2 on 表名1.列名 = 表名2.列名
 
  7:left join
  
  左外连接    select 查找信息 from 表名1 left join 表名2 on 表名1.列名 = 表名2.列名
    
 
  8:right join
 
   右外连接  select 查找信息 from 表名1 right join 表名2 on 表名1.列名 = 表名2.列名
 
  9:oracle中查询遍历树形结构(start  with)
 
  select * from extmenu
        start with pid=1
connect by prior id = pid
快速删除父节点以及父节点下的所有节点:
Delete from extmenu where id in (
elect * from extmenu
        start with pid=1
connect by prior id = pid
)
 
 
  10:查询出来60-70,80-90,95-100学生的信息
    
select * from stu where chengji between 60 and 70 or between 80 and 90 or between 95 and 100
select * from stu where chengji > 60 and chengji < 70 or chengji > 80 and chengji < 90 or chengji > 95 and chengji < 100
 
11:用exists替换in------进行联表查询
select * from dept where exists(select * from emp where emp.deptno=dept.deptno);
select  *  from  dept  d  inner  join  emp  e  on  d.deptno = e.deptno(只查询出两表共同拥有的字段数据)
 
12:删除表中的重复数据:
delete from xin a where a.rowid != (
select max(b.rowid) from xin b
where a.name = b.name
);
 
13:row_number(),rank() over ,dense_rank() over 按工资排序
  select sal,
                 row_number() over(order by sal desc) rank1,
                 rank() over(order by sal desc) rank,
                 dense_rank() over(order by sal desc) drank
            from emp
 
14:select * from (select emp.* from(
dense_rank() over(partition by departNo order by sal desc)
rk from emp )
Where rk=4

重要的sql语句

标签:sql   creat   rom   如何快速   如何   int   查询   let   事物   

原文地址:https://www.cnblogs.com/krifs/p/14390890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!