
问题描述:
表为: table1
里面字段为: id test1 test2 test3 test4
内容为: 1 百度 baidu 2006-08-01 admin
2 网易 163 2006-08-03 user
3 雅虎 .yahoo 2006-08-05 admin
4 百度 baidu 2006-08-08 user
set rs=conn.execute("select distinct test1 from table")
do while not rs.eof
response.write rs("test1")
rs.movenext
loop
这样我就得出了过滤结果:
百度
网易
雅虎
但如果我想把 test2 test3 test4字段也同时显示出来的话,我该如何做呢?
set rs=conn.execute("select distinct test1,test2,test3,test4 from table1"
以上不行的.
但如果用以下方法显示觉得也不科学.
set rs=conn.execute("select distinct test1 from table")
do while not rs.eof
set rs2=conn.execute("select*from table1 where test1 = "&rs("test1"))
response.write rs("test1")
respones.write rs2("test2")
response.write rs2("test3")
response.write rs2("test4")
rs.movenext
loop
能否有更好的方法呢?谢谢谢谢谢谢!
解析:
楼主用distinct肯定达不到所需效果。
可以用group by 分组,不过因为其他字段有重复值,只能让其他字段取一个值了
sql="select test1,max(test2) as test2,max(test3) as test3,max(test4) as test4 from table1 group by test1"
示例假设存在一个产品信息表Products,其表结构如下:
CREATE TABLE Products (ProductID int,
ProductName nvarchar (40),
Unit char(2),
UnitPrice money
)
表中数据如图:
图中可以看出,产品Chang和Tofu的记录在产品信息表中存在重复。现在要删除这些重复的记录,只保留其中的一条。步骤如下:
第一步——建立一张具有相同结构的临时表
CREATE TABLE Products_temp (ProductID int,
ProductName nvarchar (40),
Unit char(2),
UnitPrice money
)
第二步——为该表加上索引,并使其忽略重复的值
方法是在企业管理器中找到上面建立的临时表Products _temp,单击鼠标右键,选择所有任务,选择管理索引,选择新建。如图2所示。
按照图2中圈出来的地方设置索引选项
第三步——拷贝产品信息到临时表
insert into Products_temp Select * from Products此时SQL Server会返回如下提示:
服务器: 消息 3604,级别 16,状态 1,行 1
已忽略重复的键。
它表明在产品信息临时表Products_temp中不会有重复的行出现。
第四步——将新的数据导入原表
将原产品信息表Products清空,并将临时表Products_temp中数据导入,最后删除临时表Products_temp。
delete Products insert into Products select * from Products_temp drop table Products_temp这样就完成了对表中重复记录的删除。无论表有多大,它的执行速度都是相当快的,而且因为几乎不用写语句,所以它也是很安全的
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)