您的位置:首页 > 数据库

SQL 常用操作

2016-06-17 14:54 169 查看
1、单表多条插入

  insert into ApplyLoan(表名)(ApplyCode,DataType,OrderType,)select ApplyCode,3,400,ApplyUser from ZXApplyLoan where ApplyCode not in ('ZX201605040001','ZX201605040002') (字段对应)

2、rank() over()聚集查询排序

  select isdelete, * from (select ID,applycode ,isdelete ,datatype,rank() over (partition by applycode,datatype order by ID ) as IDNum from ApplyUserInfo where datatype=3 ) a where IDNum>1

3、with 多表关联查询

  ;with cte as

(select usermail,max(AddTime) as AddTime ,COUNT(0) as Num from ESFUserLoginLog with(nolock) where addtime>'2015-06-01' and addtime<'2015-06-23' group by usermail,CONVERT(VARCHAR(10),AddTime,120)

),cte2 as

(select case Finance_UserManager.NickNameFrom when '0' then '二手房金融' when '1' then '二手房集团' when '2' then '新房金融' else '新房集团' end as '集团' ,Finance_UserManager.juesename, Finance_UserManager.NickName,cte.AddTime,cte.Num from cte,Finance_UserManager where cte.usermail=Finance_UserManager.Email)select * from cte2

4、分页查询

pageIndex 当前页第一条数据的num - 1
pageSize 每页几条数据
select * from (select *,row_number() over(order by pid desc) as num from photos) as t where num > @startIndex and num<= @startIndex +@pageSize order by pid desc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: