表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:还有别的方法来实现这样的功能么
表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:还有别的方法来实现这样的功能么
(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"]
select c.试卷id,a.试卷标题,c.用户id
from a,c
where a.试卷id=c.试卷id(+)
and c.用户id=Session["usrid"] 我试了,结果集为空,而不是所有试卷标题组成的记录
from c
inner join a
on a.试卷id=c.试卷id
我要的结果集里要有当前登陆用户的id,才能对每个试卷id,判断是否有当前用户id的值,
那么总共有试卷1,2,3,n那么结果集就是形如这样:试卷id 试卷标题 当前用户id
1 w
2 w
3 null
n w
left join
select a.试卷id,a.试卷标题,c.用户id
from a,c
where a.试卷id = c.试卷id test ---选出所有参加了考试的用户和试卷
on user.用户id = test.用户id --作左连接即可
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 --作左连接即可
把left join 改为right join 就行了,不过取出来有用吗??
用上面这样的写法,如果试卷1有n个用户考了,
那么上面的sql语句,出来的结果集里就有n个记录是试卷1,而不是只有1个记录
from 试卷表 a,(select * from 考试表 where 用户 = session["userid"]) c
where a.试卷id =c.试卷id(+);
---------- ----------
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>
decode(c.用户id,session["userid"],c.用户id,null) 是什么意思,我愚钝,没看懂
decode()是oracle里面的函数,结构为decode(表达式,条件1,值1,条件2,值2,....,值N)
当 表达式 = 条件1 时 返回 值1
当 表达式 = 条件2 时 返回 值2
.....
所有的条件都不满足时返回 值N