2016-07-23 45 views
2

我有表:Users(id,name,...),Knowledge(id,name,...)Courses(id,name....)。 每個用戶都有多個「知識」(ManyToMany類型的用戶和知識鏈接)。 例如,用戶「用戶1」具有2個知識「1」和「2」。 課程還通過ManyToMany類型與知識鏈接。但在這種情況下的知識是要求。 例如,課程「課程1」需要知識「1」和「2」,用戶可以訪問該課程。 課程「課程2」需要知識「1」和「3」,且用戶無法訪問此課程。如何爲我的任務寫出正確的sql請求

我想編寫正確的sql命令,它返回所有課程,它可以獲取用戶。 我使用PostgresSql。 對不起,我可怕的英語。

我想這個SQL命令加入課程和知識:

select c.id as "Course id", k.requiredknowledge_id from course as c inner join courses_required_knowledges as k on k.course_id = c.id; 

類似的命令加入用戶和知識。

select u.id as "User id", k.knowledge_id from user_info as u inner join users_knowledge as k on k.userInfo_id = u.id where u.id = 1; 

但我不知道如何「combain」此命令爲GET結果

+1

請顯示您到目前爲止所嘗試的內容。 – Serg

+1

編輯你的問題,並提供樣本數據和期望的結果。另外,用您正在使用的數據庫標記問題。 –

+0

我想,也許concat列值(group by)並比較它 –

回答

0
select c.id as "Course", array_agg(k.requiredknowledge_id) as "Requiried knoweldge" , array_agg(uk.knowledge_id) as "User Knowledge" 
from course as c 
right join courses_required_knowledges as k on k.course_id = c.id 
right join users_knowledge as uk on uk.userInfo_id = 1 
group by c.id 
having array_agg(k.requiredknowledge_id) <@ array_agg(uk.knowledge_id); 

這對我工作的例子。

0

你可能在尋找這樣的事情。

首先,讓我們創建一些測試數據:

create table course (id int primary key, name varchar(20)); 
insert into course (id, name) values 
(1,'cooking'), 
(2,'super hero'), 
(3,'otaku'); 

create table knowledge (id int primary key, name varchar(30)); 
insert into knowledge (id, name) values 
(4,'boil egg'), 
(5,'toast bread'), 
(6,'maintain secret identity'), 
(7,'read'); 

create table courses_required_knowledges (id int primary key, course_id int, requiredknowledge_id int); 
insert into courses_required_knowledges (id, course_id, requiredknowledge_id) values 
(8,1,4), 
(9,1,5), 
(10,2,6), 
(11,3,7); 

create table user_info (id int primary key, name varchar(20)); 
insert into user_info (id, name) values 
(12,'John Doe'), 
(13,'Konata'), 
(14,'Mega Mindy'); 

create table users_knowledge (id serial primary key, userInfo_id int, knowledge_id int); 
insert into users_knowledge (userInfo_id, knowledge_id) values 
(12,4), 
(12,7), 
(12,5), 
(13,7), 
(14,6), 
(14,7), 
(14,5); 

查詢下方加入他們對記者的字段。
隨着用戶表交叉連接(全部到)所需的課程。

select 
-- course.id as course_id, 
-- coursereq.requiredknowledge_id, 
-- user_info.id as user_id, 
course.name as course, 
reqknow.name as required_knowledge, 
user_info.name as username, 
(case when users_knowledge.id is null then 'N' else 'Y' end) as user_meets_requirement 
from courses_required_knowledges as coursereq 
full outer join user_info on (1=1) 
left join users_knowledge 
    on (user_info.id = users_knowledge.userInfo_id and 
     coursereq.requiredknowledge_id = users_knowledge.knowledge_id) 
left join knowledge as reqknow 
    on (coursereq.requiredknowledge_id = reqknow.id) 
-- left join knowledge as usrknow 
-- on (users_knowledge.knowledge_id = usrknow.id) 
left join course 
    on (coursereq.course_id = course.id) 
where users_knowledge.id is null 
order by 
course.name, reqknow.name, user_info.name; 

這將給用戶錯過的要求註冊課程。

結果:

course  required_knowledge  username user_meets_requirement 

cooking  boil egg     Konata  N 
cooking  boil egg     Mega Mindy N 
cooking  toast bread    Konata  N 
super hero maintain secret identity John Doe N 
super hero maintain secret identity Konata  N 
+0

感謝您的回答,但我在ManyToMany的問題錶鏈接中犯了大錯。我將編輯我的問題 –

+0

我對這些名字有一些樂趣。 – LukStorms

相關問題