sql中的排序,如何使用倒序

sql中的排序,如何使用倒序,第1张

sql中的排序使用倒序的步骤如下:

我们需要准备的材料分别是:电脑、sql查询器。

1、首先,打开sql查询器,连接上相应的数据库表,例如test表,以score字段倒序为例。

2、点击“查询”按钮,输入:select * from test order by score desc。

3、点击“运行”按钮,此时会发现score字段按倒序排序查询出了。

你有2个查询语句,2个nSql,这样设置是矛盾的,建议改成查询语句:

Sql="Select top 10 * From [Dv_Topic] Order By TopicID DESC,boardid DESC"

这样就可以了,优先TopicID倒序再按boardid倒序,你试试!

按id号从小到大排列

sql = "select * from tablename order by id asc"

按id号从大到小排列

sql = "select * from tablename order by id desc"

举例如下:

下面是一段正常的源码,它可以正常查询出倒序结果:

<!--#include file="conn.asp" -->

<head>

<title>回眸一笑</title>

<style><!--@import url(in/index.css)--></style>

<style><!--@import url(in/layer.css)body,td,th {

font-family: 宋体

color: #000

}

--></style>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312"></head>

<body text="#000000" >

<div id="newest">

<div id="ntitle" align="right">最新10篇帖子:</div>

<div id="space"></div>

<div id="nmarquee">

<table width="100%" height="12" border="0" cellpadding="0" cellspacing="0">

<tr>

<td id=demo1>

<table width="100%" height="35" border="0" cellpadding="0" cellspacing="0">

<tr>

<%

Dim nSql

nSql="Select top 10 * From [Dv_Topic] Order By TopicID DESC"

Dim nRs

Set nRs=Server.CreateObject("ADODB.RecordSet")

nRs.Open nSql,Conn1,1,1

while not nRs.eof

%>

<td height="17"><table width="500"><tr><td width="492" height="10">

<a href='../../xiabbs/Dvbbs8.2.0_Ac/dispbbs.asp?TopicIDid=<%=nRs("TopicID")%>'>

<strong><font color="#333366"><%=left(nRs("PostUserName"),5)%></font></strong>

<font color="#FF3366">发贴于 </font>

<strong><font color="#333366"><%=left(nRs("DateAndTime"),12)%></font></strong>:<%=left(nRs("Title"),20)%>

</a></td></tr></table></td>

<%

nRs.movenext

wend

nRs.close:set nRs = nothing

%>

但是把它的链接更改,使它指向其它ID,但查询结果却顺序显示了。代码如下:

<!--#include file="conn.asp" -->

<head>

<title>回眸一笑</title>

<style><!--@import url(in/index.css)--></style>

<style><!--@import url(in/layer.css)body,td,th {

font-family: 宋体

color: #000

}

--></style>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312"></head>

<body text="#000000" >

<div id="newest">

<div id="ntitle" align="right">最新10篇帖子:</div>

<div id="space"></div>

<div id="nmarquee">

<table width="100%" height="12" border="0" cellpadding="0" cellspacing="0">

<tr>

<td id=demo1>

<table width="100%" height="35" border="0" cellpadding="0" cellspacing="0">

<tr>

<%

Dim nSql

nSql="Select top 10 * From [Dv_Topic] Order By TopicID DESC"

nSql="Select top 10 * From [Dv_Topic] Order By boardid DESC"<!--这里添加了查询表,以为下面链接提供ID链接-->

Dim nRs

Set nRs=Server.CreateObject("ADODB.RecordSet")

nRs.Open nSql,Conn1,1,1

while not nRs.eof

%>

<td height="17"><table width="500"><tr><td width="492" height="10">

<!--下面的链接添加了boardid以便动态链接。-->

<a href='../../xiabbs/Dvbbs8.2.0_Ac/dispbbs.asp?boardid=<%=nRs("boardid")%>&id=<%=nRs("TopicID")%>'>

<strong><font color="#333366"><%=left(nRs("PostUserName"),5)%></font></strong>

<font color="#FF3366">发贴于 </font>

<strong><font color="#333366"><%=left(nRs("DateAndTime"),12)%></font></strong>:<%=left(nRs("Title"),20)%>

</a></td></tr></table></td>

<%

nRs.movenext

wend

nRs.close:set nRs = nothing

%>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9561232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存