sql 题目

1.自增列

通用:

select id=(select count(1) from table b where b.sid<a.sid) ,* from table a;
select id=identity(int,1,1),* from ...

第二个已经有主键自增列的就不可以用了

还有就是rownumber

2.

 CREATE TABLE dbo.#testTab 
 ( 
  Id int NOT NULL
 ) 

添加数据:

  insert into #testTab values(3);
 insert into #testTab values(4);
 insert into #testTab values(7);
 insert into #testTab values(10);
 insert into #testTab values(11);
 insert into #testTab values(19);
 insert into #testTab values(20);
 insert into #testTab values(21);
 insert into #testTab values(26);
  insert into #testTab values(27);

 select s2.id+1,s3.id-1 from (
 select * from ( select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
 select t1.Id,
 t2.Id as UpId,t3.id as NextId from #testTab as t1
 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1)
 where t2.Id is null or t3.Id  is null) as t4
 ) as s1 where  (s1.init < (
  select COUNT(1) from #testTab as t1 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1) where t2.Id is null or t3.Id  is null
 ) and s1.UpId is null) or (s1.UpId is null and s1.NextId is null)
 ) as s2 left join
 (select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
 select t1.Id,
 t2.Id as UpId,t3.id as NextId from #testTab as t1
 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1)
 where t2.Id is null or t3.Id  is null) as t4) s3 
 on s2.init  = s3.init-1
 where s3.Id is not null

 

 其实就是查出id列排序后数据之间的间隔数据的最小与最大值

 分析:

 //1 t4
 select t1.Id,t2.Id as UpId,t3.id as NextId from #testTab as t1
 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1)
 where t2.Id is null or t3.Id  is null

//2 s1
 select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
 select t1.Id,t2.Id as UpId,t3.id as NextId from #testTab as t1
 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1)
 where t2.Id is null or t3.Id  is null)as t4

 //3 s2 //获取间隔数据中小的
 select * from ( select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
 select t1.Id,t2.Id as UpId,t3.id as NextId from #testTab as t1
 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1)
 where t2.Id is null or t3.Id  is null) as t4
 ) as s1 where  (s1.init < (
  select COUNT(1) from #testTab as t1 left join  #testTab as t2 on t1.Id = (t2.Id-1) 
 left join  #testTab as t3 on t1.Id = (t3.Id+1) where t2.Id is null or t3.Id  is null
 ) and s1.UpId is null) or (s1.UpId is null and s1.NextId is null)

这一步是最重要的,它的思路是:

先找出间隔中比较小的那端

s1.init<(.......)and s1.UpId is null  是为了这里的话就是26,27,剔除不是间隔的数据

s1.UpId is null and s1.NextId is null 是为了如果这里26,27只有一位的话,它其实是不能被剔除的,它是间隔的数据

 最后就是通过间隔中较小的数据的init+1来查找间隔的较大数据,其实s3就是s1,左连进就可以了。

3.sql%余数

要整除时就要最后一个数字,就是不能为0

类似其他的Mod

select case 2%count(1) when 0 then count(1) else 2%count(1) end from Alliance_B2BContacter where IsShow='T'

这个里面就是一个随机数(这里是2)整除表数量后的序号,不能为0,当为0是就取表最后一条数据。

转载于:https://www.cnblogs.com/hongdada/p/3213908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值