求几个SQL问题,谢谢大家帮忙

0
有四张表:问题表(aws_question)、话题表(aws_topic)、分类表(aws_category)、 问题与话题关联表(aws_question_topic_relation)
对应的表结构如下:
问题表(aws_question)
question_id、question_content、category_id
------  问题表 ----
create table aws_question
(
  question_id int null,
  question_content varchar(50),
  category_id int null
);

insert into aws_question(question_id,question_content,category_id)
VALUES (1,'商业智能问题111',1);
insert into aws_question(question_id,question_content,category_id)
VALUES (2,'商业智能问题222',1);
insert into aws_question(question_id,question_content,category_id)
VALUES (3,'商业智能问题333',1);
insert into aws_question(question_id,question_content,category_id)
VALUES (4,'Cognosssss333',3);
insert into aws_question(question_id,question_content,category_id)
VALUES (5,'biee测试',4);
commit;


话题表(aws_topic)
topic_id、topic_title
------  话题表  ----
create table aws_topic
(
  topic_id int null,
  topic_title varchar(50)

);

insert into aws_topic(topic_id,topic_title)
VALUES (5,'商业智能');
insert into aws_topic(topic_id,topic_title)
VALUES (10,'ETL');

commit;


分类表(aws_category)
id、title
------  分类表  创建表结构----
create table aws_category
(
  id int null,
  title varchar(50)

);

insert into aws_category(id,title)
VALUES (1,'商业智能');
insert into aws_category(id,title)
VALUES (2,'ETL');
insert into aws_category(id,title)
VALUES (3,'Cognos');
insert into aws_category(id,title)
VALUES (4,'BIEE');
commit;


问题与话题关联表(aws_question_topic_relation)
id、topic_id、question_id
------  关联表  (话题ID 和问题ID是唯一键)----
create table aws_question_topic_relation
(
  id int null,
  topic_id int,   ---话题ID
  question_id int null  ---问题ID
);

insert into aws_question_topic_relation(id,topic_id,question_id)
VALUES (1,5,1);
insert into aws_question_topic_relation(id,topic_id,question_id)
VALUES (2,10,1);
insert into aws_question_topic_relation(id,topic_id,question_id)
VALUES (3,10,2);

commit;


要实现的功能如下:
1、用SQL实现往 话题和问题的关联表(aws_question_topic_relation)里面插入数据,规则是:问题属于 商业智能 分类的,把该问题的话题也添加为 商业智能,添加前记得判断已存在商业智能的话题,就不要添加
 
插入后的结果如下:
ID   topic_id question_id
1 5 1
2 10 1
3 10 2

4 5 2
5 5 3


2、如果该问题的话题 属于 BIEE启动报错、BIEE数据源、BIEE数据源连接 等3类话题的,请把该问题的话题修改为 BIEE 安装配置,谢谢。
已邀请:
1

- 取是能力,舍是境界 2015-10-24 回答

问题1:
insert into aws_question_topic_relation(topic_id,question_id)
select aws_topic.topic_id,question_id
from aws_question
join aws_category on aws_question.category_id = aws_category.id
join aws_topic on aws_category.title = aws_topic.topic_title
where not exists(
select * from aws_question_topic_relation
where topic_id = aws_topic.topic_id and question_id = aws_question.question_id
)
问题2:
先从话题表中找到BIEE启动报错、BIEE数据源、BIEE数据源连接、BIEE 安装配置这几个话题的ID。比如分别是1、3、4、9。
 update aws_question_topic_relation set topic_id = 9 where topic_id in(1,3,4)
1

老头子 - 专注是唯一的捷径 2015-10-23 回答

---1
select q.question_id, t.topic_id
  from aws_question q
 inner join aws_category c
    on q.category_id = c.id
 inner join aws_topic t
    on c.title = t.topic_title
 where c.title = '商业智能'
   and q.question_id || t.topic_id not in
       (select question_id || topic_id from aws_question_topic_relation qt);
 
 
--2
update aws_question_topic_relation t
set t.topic_id = 1234567 --BIEE 安装配置 的id
where t.topic_id in (1,2,3) --BIEE启动报错、BIEE数据源、BIEE数据源连接 的id
0

hbdn521 - 没什么好说的 2015-10-23 回答

MSSQL写法  先献丑了
2、
--先查找匹配数据
select * from aws_question a,aws_topic b,aws_question_topic_relation c
where a.question_id=c.question_id and b.topic_id=c.topic_id
and b.topic_title in ('BIEE启动报错','BIEE数据源','BIEE数据源连接')
--更新匹配数据
update b set topic_title='BIEE 安装配置' from aws_question a,aws_topic b,aws_question_topic_relation c
where a.question_id=c.question_id and b.topic_id=c.topic_id
and b.topic_title in ('BIEE启动报错','BIEE数据源','BIEE数据源连接')

要回复问题请先登录注册