惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
V
V2EX
PCI Perspectives
PCI Perspectives
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 叶小钗
V
Visual Studio Blog
Jina AI
Jina AI
P
Proofpoint News Feed
罗磊的独立博客
SecWiki News
SecWiki News
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
T
Tailwind CSS Blog
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
G
GRAHAM CLULEY

博客园 - lswweb

Drone概念与答疑 Drone基于K8S的Secrets管理 Centos8通过X11使用GUI应用 Linux自己常用命令记录(待续) K8S Master节点灾备与恢复 - ETCD K8S Master节点灾备与恢复 - Host文件Copy Rook Ceph出现daemons have recently crashed异常 Ceph rbd删除image遭遇watchers异常处理 Ceph OSD更换硬盘后遭遇PG Inconsistent异常与处理 Rook Ceph OSD异常,格式化osd硬盘重新挂载 进入K8S Pod执行操作,遭遇Operation not permitted错误 K8S Pod挂载rook ceph pv报not found in the list of registered CSI drivers异常处理 Vs中调试MVC源代码步骤 手动创建vsct文件 快速查找找VS菜单信息(Guid、GuidId、CmdId等) Knockout中ViewModel与Model的互转 Asp.Net MVC 表单验证 AjaxOptions.OnSuccess回调方法返回的参数信息 一次自定义Configuration的悲惨经历。
在多排序条件下SQL获文章上一条、下一条记录
lswweb · 2015-01-04 · via 博客园 - lswweb

        在我们开发CMS系统的时候经常会碰到需要根据当前记录来读取它的上一条记录或下一条记录的现像。单字段排序时SQL语句非常简单,但是当以多字段排序时应该怎么写呢?实际上也复杂,多字段无法搞定那么我们就需要将多个字段合并在一起,形成一个固定长度的字符串,再将这个生成的字符串做为排序字段进行排序。注意,这个排序字符串的长度一定要是固定的,所有记录生成的排序字符串长度一至才能准确进行排序。

表结构:

CREATE TABLE [table1](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Sort] [int] NOT NULL,
    [CreatedOn] [datetime] NOT NULL,
CONSTRAINT [PK_table1] PRIMARY KEY 
(
    [Id] ASC
) ON [PRIMARY]

        在这个表中Id是主键,我们用Id做为条件来查询当前记录,以Sort和CreatedOn两个字段做为排序字段。Sort是数字型值为1-9999,因为它的最大值是四位数9999,为了保证我们生成的字符串长度是一至的,所以我们把它转换生成一个5位的字符串。如:10000+1=10001、10000+9999=19999。

上一条:

SELECT 
     top 1 *,(convert(varchar,10000 + sort) + convert(varchar,createdOn,120)) as a1
FROM 
    table1 
WHERE 
    (convert(varchar,10000 + sort)+ convert(varchar,createdOn,120)) < (select a1=(convert(varchar,10000 + sort)+ convert(varchar,createdOn,120)) from table1 where id=5) 
order by a1 desc;

下一条:

SELECT 
    TOP 1 *,(convert(varchar,10000 + sort) + convert(varchar,createdOn,120)) as a1
 FROM 
table1
WHERE
(
convert(varchar,10000 + sort) + convert(varchar,createdOn,120)) > (select a1=(convert(varchar,10000 + sort) + convert(varchar,createdOn,120)) from table1 where id=2)
order by a1 asc;