首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL字段输出记录循环控制请问

2012-08-22 
SQL字段输出记录循环控制请教表名:test字段id qid tid text103asp213php313aspx413js523jsp623htmlSQL语句

SQL字段输出记录循环控制请教
表名:test
字段
id qid tid text
1 0 3 asp
2 1 3 php
3 1 3 aspx
4 1 3 js
5 2 3 jsp
6 2 3 html

SQL语句:

SQL code
set rsc=server.createobject("adodb.recordset")rsc.open "select * from test where qid="&ID&" and TID="&TID  ,conn,1,1If Not rsc.EOF and Not rsc.bof Thendo while not rsc.eofResponse.Write rsc("txet")rsc.movenextloopend ifrsc.close :set rsc=nothing


以上语句能输出QID等于ID的全部记录,现在要求QID为1和2的字段均输出2条记录,请教大家了

[解决办法]
SQL code
--> 测试数据:[tb]IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]GOCREATE TABLE [tb]([id] INT,[qid] INT,[tid] INT,[text] VARCHAR(4))INSERT [tb]SELECT 1,0,3,'asp' UNION ALLSELECT 2,1,3,'php' UNION ALLSELECT 3,1,3,'aspx' UNION ALLSELECT 4,1,3,'js' UNION ALLSELECT 5,2,3,'jsp' UNION ALLSELECT 6,2,3,'html'GO--> 测试语句:SELECT * FROM [tb]  as a where (select count(*) from tb where [qid]=a.[qid] and [id]<=a.[id])<=2/*id          qid         tid         text----------- ----------- ----------- ----1           0           3           asp2           1           3           php3           1           3           aspx5           2           3           jsp6           2           3           html(5 行受影响)*/
[解决办法]
SQL code
SELECT * FROM [tb]  as a where (select count(1) from tb where [qid]=a.[qid] and [id]<=a.[id] and a.[qid]=[id])<=2 

热点排行
Bad Request.