求几个SQL问题,谢谢大家帮忙
0
有四张表:问题表(aws_question)、话题表(aws_topic)、分类表(aws_category)、 问题与话题关联表(aws_question_topic_relation)
对应的表结构如下:
问题表(aws_question)
question_id、question_content、category_id
话题表(aws_topic)
topic_id、topic_title
分类表(aws_category)
id、title
问题与话题关联表(aws_question_topic_relation)
id、topic_id、question_id
要实现的功能如下:
1、用SQL实现往 话题和问题的关联表(aws_question_topic_relation)里面插入数据,规则是:问题属于 商业智能 分类的,把该问题的话题也添加为 商业智能,添加前记得判断已存在商业智能的话题,就不要添加
插入后的结果如下:
2、如果该问题的话题 属于 BIEE启动报错、BIEE数据源、BIEE数据源连接 等3类话题的,请把该问题的话题修改为 BIEE 安装配置,谢谢。
对应的表结构如下:
问题表(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 安装配置,谢谢。
没有找到相关结果
重要提示:提问者不能发表回复,可以通过评论与回答者沟通,沟通后可以通过编辑功能完善问题描述,以便后续其他人能够更容易理解问题.
3 个回复
悟 - 取是能力,舍是境界 2015-10-24 回答
赞同来自: 梁勇
问题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)
老头子 - 专注是唯一的捷径 2015-10-23 回答
赞同来自: 梁勇
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
hbdn521 - 没什么好说的 2015-10-23 回答
赞同来自:
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数据源连接')