Use SQL query to check if an entry contains a list of values and compare
P粉270842688
2023-08-30 17:47:36
<p>I have two MySql tables with the following schema</p>
<pre class="brush:php;toolbar:false;">Courses(CNO:CHAR(10),CName:VARCHAR(50), DeptId:CHAR(3)): Each course has a unique ID called CNO
Register(RNO:CHAR(10), CNO:CHAR(10), Year:INTEGER, Sem:INTEGER, Grade:CHAR)</pre>
<p>The current task is to find all tuples that satisfy (r1, r2, d), in which the student with student ID r1 has completed all the DeptIds completed by the student with student ID r2 as <strong>ID</ Strong>'s department's courses. </p>
<p>My method is to first find all the courses completed in the department by the student with student number r2, and then similarly find all the courses completed in the department by the student with student number r1, and compare whether they are the same. But I don't know if this is correct or how to compare the two results. </p>
<p>This is the code I use to check all courses completed by the student with middle school number r2 in a specific department: </p>
<p>Here is the link to the code and data DB Fiddle</p>
This is my method
step 1: Create a table with two columns, student1 and student2, containing unique combinations of students
For example:
Step 2: Create a table adding a unique DeptId
for each different DeptId from the previous tableFor example:
Step 3: Finally, only the tuples in which RNO1 and RNO2 have taken the same course in each DeptId are retained.
The following is an example where rno1_rno2_deptid refers to the table above
The above only selects the case where RNO1 and RNO2 have the same CNO in the same series.