表a:试卷表,字段:试卷id,试卷标题
表b:用户表,字段:用户id
表c:考试表,字段:考试表流水id,试卷id,用户id当前登陆用户id保存在Session["usrid"]
当用户答题完,则向考试表中添加一行数据,该试卷id,该用户id。要实现功能:1:结果集要显示所有试卷的标题
            2:对所有的试卷,判断当前登陆用户的答题情况我的思路是这样:结果集:试卷id    试卷标题       当前登陆用户id
1            1             Session["usrid"] (已答该试卷)
2            2             null (未答该试卷)
3            3             null (未答该试卷) 
,           ,            ,
n            n             Session["usrid"] (已答该试卷)这样就可以通过结果集中的当前登陆用户id字段进行判断,如果为空,就是未答题,
可是sql语句写不来,想了很久了,不知道如何实现。ps:还有别的方法来实现这样的功能么

解决方案 »

  1.   

    1.用union
    (select a.试卷id ,a.试卷标题,c.用户id
    from a,c 
    where a.试卷id=c.试卷id and c.用户id=Session["usrid"] 
    union
    select a.试卷id,a.试卷标题,null 
    from a 
    where a.试卷id not in (select 试卷id from c)
    )
    order by a.试卷id 
    2用外联接
    select c.试卷id,a.试卷标题,c.用户id
    from a,c 
    where a.试卷id=c.试卷id(+)
    and c.用户id=Session["usrid"] 
      

  2.   

    用外联接
    select c.试卷id,a.试卷标题,c.用户id
    from a,c 
    where a.试卷id=c.试卷id(+)
    and c.用户id=Session["usrid"] 我试了,结果集为空,而不是所有试卷标题组成的记录
      

  3.   

    select a.*,c.用户id
    from c
    inner join a
    on a.试卷id=c.试卷id
      

  4.   

    guanjm(st.human) ,你的写法不对,得不到我得结果集,我的结果集里有一个字段:当前登陆用户id,意思就是:记录集要得到所有的试卷记录和当前登陆用户,并且对这些所有的试卷记录,在考试表中对应的去判断当前用户,有记录就是该用户Session,没记录就是null。
      

  5.   

    select a.试卷id ,a.试卷标题,c.用户id from a,c where a.试卷id =c.试卷id(+);
      

  6.   

    wfeng7907(无风),那session["usrid"]呢? 
    我要的结果集里要有当前登陆用户的id,才能对每个试卷id,判断是否有当前用户id的值,
      

  7.   

    我的c表(考试表)一开始一定是空的,当有用户登陆,考试完,才会向c表(考试表)写入一条记录,(该试卷id,该用户id)
      

  8.   

    比如当前登陆用户是w
    那么总共有试卷1,2,3,n那么结果集就是形如这样:试卷id  试卷标题   当前用户id
    1                     w
    2                     w   
    3                     null
    n                     w   
      

  9.   

    select a.试卷id ,a.试卷标题,decode(c.用户id,session["userid"],c.用户id,null) from 试卷表 a,考试表 c where a.试卷id =c.试卷id(+);上面即可实现楼主的效果
      

  10.   

    decode(c.用户id,session["userid"],c.用户id,null) 是什么意思,我愚钝,没看懂
      

  11.   

    这样:select b.用户id from b where b.用户id = session["usrid"]  user
    left join 
    select a.试卷id,a.试卷标题,c.用户id
    from a,c
    where a.试卷id = c.试卷id test      ---选出所有参加了考试的用户和试卷
    on user.用户id = test.用户id --作左连接即可
      

  12.   

    呵呵,忘记外面的select了:
    select test.试卷id,test.试卷标题,user.用户id from
    select b.用户id from b where b.用户id = session["usrid"]  user
    left join 
    select a.试卷id,a.试卷标题,c.用户id
    from a,c
    where a.试卷id = c.试卷id test      ---选出所有参加了考试的用户和试卷
    on user.用户id = test.用户id --作左连接即可
      

  13.   

    如果要得到这样的结果‘3                     null’
    把left join 改为right join 就行了,不过取出来有用吗??
      

  14.   

    select a.试卷id ,a.试卷标题,decode(c.用户id,session["userid"],c.用户id,null) from 试卷表 a,考试表 c where a.试卷id =c.试卷id(+);
    用上面这样的写法,如果试卷1有n个用户考了,
    那么上面的sql语句,出来的结果集里就有n个记录是试卷1,而不是只有1个记录
      

  15.   

    select a.试卷id ,a.试卷标题,用户id 
    from 试卷表 a,(select * from 考试表 where 用户 =  session["userid"]) c 
    where a.试卷id =c.试卷id(+);
      

  16.   

    SQL> select * from test2;SJID       SJTITLE
    ---------- ----------
    1          1
    2          2
    3          3SQL> select * from test3;LSID       SJID       USERID
    ---------- ---------- ----------
    1          1          a
    1          1          b
    1          2          cSQL> select test2.sjid,test2.sjtitle,a.userid from test2,
      2  (select sjid,userid from test3 where userid='a') a
      3  where test2.sjid=a.sjid(+);SJID       SJTITLE    USERID
    ---------- ---------- ----------
    1          1          a
    2          2
    3          3SQL>
      

  17.   

    回复人: annio() ( ) 信誉:99  2005-7-6 12:39:59  得分: 0  
        decode(c.用户id,session["userid"],c.用户id,null)   是什么意思,我愚钝,没看懂
    decode()是oracle里面的函数,结构为decode(表达式,条件1,值1,条件2,值2,....,值N)
    当 表达式 = 条件1 时 返回 值1 
    当 表达式 = 条件2 时 返回 值2
    .....
    所有的条件都不满足时返回  值N