asp sql 跨表查询

来源:百度知道 编辑:UC知道 时间:2024/06/09 14:04:09
表一:

id name clo
1 a c
2 b3 db
3 b2 db
4 b4 dt

表二

id2 name2 uid
3 * 2
4 * 2
5 * 2
6 * 3
7 * 3
8 * 3

查询从 表一 查到 clo=ab 的两条记录,得到id。
然后查询 表二 取到uid=id的 前两条记录。
结果如下:
3 * 2
4 * 2
6 * 3
7 * 3

sql 语句怎么实现?
rownum sql server2000 好像不支持啊。

select * from 表二 t2 where t2.uid in
(select id from 表一 t1
where t1.id = 'db')
and rownum < 3 order by t2.id2;

就是2次查询么..怎么说跨表呢
sql="select * from 表一 where clo="ab"
查找表一里符合ab的记录
rs.open sql,conn,1,1

while not rs.eof
循环读取相应记录
sql="select top 2 * from 表二 where uid="&rs("id")&""
rs1.open sql,conn,1,1
根据循环出来的id来查找表二符合id的记录
while not rs1.eof
response.write rs1("name2")
rs1.movenext
wend
rs.movenext
wend

select top 2 * from T2 where uid in
(
select top 2 id from T1 where clo = 'ab' --从 表一 查到 clo=ab 的两条记录
)然后查询 表二 取到uid=id的 前两条记录

select id2,name2,uid from(
select t2.id2,t2.name2,t2.uid,row_number()over(partition by uid order by id2) row_ from table2 t2, table1 t1 whe