0

I have 3 tables T1,T2 and T3. Table T1 contains 2 columns (key,class/student). The column class/student contains both classes and students, for example: 'english', 'math', 'mark', 'tom'... Table T2 contains 2 columns (class,student). Each class have more than one student in it, and these 2 columns use the keys from T1. In Table T3 I want to insert a specific class with its student(s) - class(es) into column A and student(s) into column B. Knowing that these columns use the keys from T1 table I've tried this but it returns same specific class with its students multiple times:

INSERT INTO T3 (A,B) 
    SELECT m.class, m.student 
    FROM T1 b,T2 m
    WHERE m.class = (SELECT key FROM T1 WHERE class/student='English') 
      AND b.KEY = m.student;

the result i get: 1 is id of class english ,10 is id of student mark, 11 is id of student tom

enter image description here

table T1:

enter image description here

Table T2:

enter image description here

2
  • 2
    You need to familiarize yourself with JOINS, there is plenty of information online that can be a great place to start. You need to understand these concepts instead of coming here looking for an answer. Better to teach a man to fish than to give him a fish, even better if he can learn to teach himself how to fish. Commented Apr 12, 2019 at 14:58
  • i have edited my post Commented Apr 12, 2019 at 15:13

2 Answers 2

1

Since your master table T1 has both class and student details, I would recommend joining it twice with the class_student_map table T2 to get the details:

INSERT INTO T3 (A,B) 

SELECT "class_master"."key", "student_master"."key"
FROM 
T2 "class_student_map"
INNER JOIN
T1 "class_master"
ON
"class_master"."key" = "class_student_map"."class"
AND
"class_master"."class/student" = 'English'
INNER JOIN
T1 "student_master"
"student_master"."key" = "class_student_map"."student";
Sign up to request clarification or add additional context in comments.

3 Comments

SQL Error: ORA-00904: "class_student_map"."class": invalid identifier, plus in T3 A and B are keys of class/student
Your second point is correct. I have updated the query now. But, the error says, whatever the table (in this case T2) referred by class_student_map doesn't have an attribute class. Can you double check table names and attribute names once?
I still didn't find anything wrong with the query. So, I have just edited it now to have double quotes everywhere i used alias. Can you try it now..?
1

Here you have simple code and just one join operation, which positively impacts on performance:

insert into t3 
WITH spec_class AS
(select key from T1 where class_student='English')
SELECT m.class, m.student 
FROM T2 m inner join spec_class sp on m.class=sp.key;

1 Comment

Hi! Please explain why this is a solution to OPs code as code only answers are discouraged on SO. This would help OP and future visitors to the site. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.