数据库查询语言中DISTINCT的问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 23:44:09
<%
page=clng(request("page"))
Set rs= Server.CreateObject("ADODB.Recordset")
if Smallclass<>"" then
sql="select DISTINCT BigClassName,FirstImageName,id,SmallClassName from news where SmallClassName='"&Smallclass&"'"
end if
rs.open sql,conn,1,1
if rs.eof and rs.bof then
response.write "<p align='center'>对不起,没有找到相关信息</p>"
else
rs.PageSize=12
if page=0 then page=1
pages=rs.pagecount
if page > pages then page=pages
rs.AbsolutePage=page
%>
这样写所有的数据都显示出来,包括BigClassName字段中有重复的都显示,现在我只想显示BigClassName字段中的一个,写成
sql="select DISTINCT BigClassName from news where SmallClassName='"&Smallclass&"'"
就对了,但是这样写FirstImageName,id,SmallClassName又丢失了
请问应该怎么写

select BigClassName,max(FirstImageName),max(id),max(SmallClassName)
from news
where SmallClassName='"&Smallclass&"'"
group by BigClassName

max()是聚合里面取最大的
你可以改成相应你要的聚合函数

如同逐鹿传说,所说你的业务需求怪怪的

你不太理解 DISTINCT 的意思。 不知道你这样写是为了实现什么业务需求。