例如有三张表
第一张:表a 为 题库 里面包含了每道题的详细内容
表a: qid题编号 qcont题内容 qtime创建时间
qid qcont qtime
1001 世界上人口最多的国家? 20090204
1002 中国的国宝是什么? 20090204
1003 中国的举办奥运会的时间是什么? 20090204第二张 表b是为学生所定制的题目
表b: userid用户编号 qid题编号(引用表a的)
userid qid
1212 1001
1212 1002第三张 表c是为学生已经回答过的题目答案
表c: userid用户编号 qid题编号(引用表a的) ans答案 time 回答时间
userid qid ans time
1212 1001 中国 20090205
1212 1002 熊猫 20090205现在我的问题是:有一天,管理员将重新为学生定制的题目 也就是将表b的内容
修改为:
userid qid
1212 1003学生查看以前做过的题目的时候也顺便把新定制的题目也显示出来 这样的SQL语句该怎么去写啊???
例如:1001 世界上人口最多的国家?
中国
1002 中国的国宝是什么?
熊猫
1003 中国的举办奥运会的时间是什么?
第一张:表a 为 题库 里面包含了每道题的详细内容
表a: qid题编号 qcont题内容 qtime创建时间
qid qcont qtime
1001 世界上人口最多的国家? 20090204
1002 中国的国宝是什么? 20090204
1003 中国的举办奥运会的时间是什么? 20090204第二张 表b是为学生所定制的题目
表b: userid用户编号 qid题编号(引用表a的)
userid qid
1212 1001
1212 1002第三张 表c是为学生已经回答过的题目答案
表c: userid用户编号 qid题编号(引用表a的) ans答案 time 回答时间
userid qid ans time
1212 1001 中国 20090205
1212 1002 熊猫 20090205现在我的问题是:有一天,管理员将重新为学生定制的题目 也就是将表b的内容
修改为:
userid qid
1212 1003学生查看以前做过的题目的时候也顺便把新定制的题目也显示出来 这样的SQL语句该怎么去写啊???
例如:1001 世界上人口最多的国家?
中国
1002 中国的国宝是什么?
熊猫
1003 中国的举办奥运会的时间是什么?
第一张:表a 为 题库 里面包含了每道题的详细内容
表a: qid题编号 qcont题内容 qtime创建时间
qid qcont qtime
1001 世界上人口最多的国家? 20090204
1002 中国的国宝是什么? 20090204
1003 中国的举办奥运会的时间是什么? 20090204 第二张 表b是为学生所定制的题目
表b: userid用户编号 qid题编号(引用表a的)
userid qid
1212 1001
1212 1002 第三张 表c是为学生已经回答过的题目答案
表c: userid用户编号 qid题编号(引用表a的) ans答案 time 回答时间
userid qid ans time
1212 1001 中国 20090205
1212 1002 熊猫 20090205 现在我的问题是:有一天,管理员将重新为学生定制的题目 也就是将表b的内容
修改为:
userid qid
1212 1003 学生查看以前做过的题目的时候也顺便把新定制的题目也显示出来,如果没有新的定制题目就只显示已经做的题目 这样的SQL语句该怎么去写啊???
例如: 1001 世界上人口最多的国家?
中国
1002 中国的国宝是什么?
熊猫
1003 中国的举办奥运会的时间是什么?
drop table a
Go
Create table a([qid] int,[qcont] nvarchar(15),[qtime] Datetime)
Insert a
select 1001,N'世界上人口最多的国家?','20090204' union all
select 1002,N'中国的国宝是什么?','20090204' union all
select 1003,N'中国的举办奥运会的时间是什么?','20090204'
if not object_id('b') is null
drop table b
Go
Create table b([userid] int,[qid] int)
Insert b
select 1212,1001 union all
select 1212,1002 union all
select 1212,1003
if not object_id('c') is null
drop table c
Go
Create table c([userid] int,[qid] int,[ans] nvarchar(2),[time] Datetime)
Insert c
select 1212,1001,N'中国','20090205' union all
select 1212,1002,N'熊猫','20090205'select a.*,c.ans,c.time from a
inner join b
on a.qid=b.qid
left join c
on c.userid=b.userid and c.qid=a.qid/*
qid qcont qtime ans time
----------- --------------- ------------------------------------------------------ ---- ------------------------------------------------------
1001 世界上人口最多的国家? 2009-02-04 00:00:00.000 中国 2009-02-05 00:00:00.000
1002 中国的国宝是什么? 2009-02-04 00:00:00.000 熊猫 2009-02-05 00:00:00.000
1003 中国的举办奥运会的时间是什么? 2009-02-04 00:00:00.000 NULL NULL(所影响的行数为 3 行)*/
select @str=isnull(@str,'')+char(13)+convert(varchar,a.qid)+space(2)+a.qcont+char(13)+isnull(c.ans,'') from a
inner join b
on a.qid=b.qid
left join c
on c.userid=b.userid and c.qid=a.qid
select @str/*
---------------------------------------------------------------------
1001 世界上人口最多的国家?
中国
1002 中国的国宝是什么?
熊猫
1003 中国的举办奥运会的时间是什么?
(所影响的行数为 1 行)
*/
if object_id('[a]') is not null drop table [a]
go
create table [a]([qid] int,[qcont] varchar(29),[qtime] datetime)
insert [a]
select 1001,'世界上人口最多的国家?','20090204' union all
select 1002,'中国的国宝是什么?','20090204' union all
select 1003,'中国的举办奥运会的时间是什么?','20090204'
if object_id('[b]') is not null drop table [b]
go
create table [b]([userid] int,[qid] int)
insert [b]
select 1212,1001 union all
select 1212,1002
if object_id('[c]') is not null drop table [c]
go
create table [c]([userid] int,[qid] int,[ans] varchar(4),[time] datetime)
insert [c]
select 1212,1001,'中国','20090205' union all
select 1212,1002,'熊猫','20090205'
insert into b values(1212,1003)
---查询---
select b.qid,a.qcont,isnull(c.ans,'') as ans
from b
left join a on a.qid=b.qid
left join c on c.qid=b.qid
where b.userid='1212'---结果---
qid qcont ans
----------- ----------------------------- ----
1001 世界上人口最多的国家? 中国
1002 中国的国宝是什么? 熊猫
1003 中国的举办奥运会的时间是什么? (所影响的行数为 3 行)
---查询---
select ltrim(b.qid)+' '+a.qcont+char(13)+isnull(c.ans,'')
from b
left join a on a.qid=b.qid
left join c on c.qid=b.qid
where b.userid='1212'---结果---
------------------------------------------------
1001 世界上人口最多的国家?
中国
1002 中国的国宝是什么?
熊猫
1003 中国的举办奥运会的时间是什么?
根本不会显示 显示以前做过的题目