注册 登录
编程论坛 ASP技术论坛

关于两个表取数显示的问题

popqi863 发布于 2010-04-05 09:56, 447 次点击
数据库中有两个表
A:   id(自动) name1 name2 name3
        1       xx1    xx2    xx3
        2       xxx1   xxx2   xxx3
        3       xxxx1  xxxx2  xxxx3
B:  u_id(和ID对应) col1 col2 col3
         1      aa      aa1    aa2
         1      bb      bb1    bb2
         2      aaa     aaa1   aaa2
         2      aa1     ss1    ss2
         2      11      22     33
         3      11      11     11
         3      22      22      22
利用语句 select * from a b where id=u_id and id="&id
现在如何把 A   B表的值显示在网页上哪? 这样查询可以吗?这的用个循环吧 如何写

[ 本帖最后由 popqi863 于 2010-4-5 10:47 编辑 ]
2 回复
#2
nicechlk2010-04-05 14:23
有2个方案,第一个:
先列出A表的值:
dim rs
set rs=conn.execute ("select * from A")
if not (rs.eof and rs.bof) then   '如果记录存在则:
    do while not rs.eof   ’开始循环
    dim aID
    aID=rs(id)
    '根据A表的ID值,在B表中循环查找(条件查询)
    dim rs1
    set rs1=conn.execute ("select * from B where u_id="&aID)
    if rs1.eof and rs1.bof then
        response.write "对不起,数据不存在!"
    else
        dim cur_id
        do while not rs1.eof
        cur_id=rs1("u_id")
        response.write cur_id(或者是cur_id对应的其他字段)
        rs1.movenext
        loop
        rs1.close
        set rs1=nothing
    end if
    rs.movenext
    loop
    rs.close
    rs=nothing
end if
#3
nicechlk2010-04-05 14:33
第二种方法:多表联合查询
dim rs,sql
sql="select id from A as C inner join B as D on A.id=B.u_id"
set rs=conn.execute ("sql")
1