问题
I have got two tables named "studentBio" and "subjects". Fields of both tables are being given below(with some values): On the form i have got something like this:

Fields of studentBio table are given as follows: WHERE (RollNo, RegYear,program, and faculty combine to make composite primary key):
RollNo RegYear stuName program faculty phoneNuber Address 1 2010 John Intermediate Pre-Engineering 343483834 London 2 2011 Leonard Intermediate Pre-Medical 454545445 NewYork 3 2012 Henry Graduation B.A 565656565 Oslo
Similary fields of subjects table are as follows: WHERE (RollNo, RegYear,program, and faculty combine to make composite primary key):
RollNo RegYear program faculty subjectName 1 2010 Intermediate Pre-Engineering A 1 2010 Intermediate Pre-Engineering B 1 2010 Intermediate Pre-Engineering C 2 2011 Intermediate Pre-Medical D 2 2011 Intermediate Pre-Medical E 2 2011 Intermediate Pre-Medical F
and so on. Now lets come to the problem. What i am doing is UPDATING (faculty and program) in both tables and subjectN in subjects table. What i have done so far is that i have updated only studentBio table which is quite easy but i can't figure-out as how could i structure my update query to update subjects table? Can somebody please help me structuring the query?
回答1:
If I understood well, please try this restructured version of your query:
string update_Personal = "UPDATE studentBio INNER JOIN subjects ON studentBio.RollNo = subjects.RollNo AND studentBio.RegYear = subjects.RegYear AND studentBio.program = subjects.program AND studentBio.faculty = subjects.faculty SET studentBio.program = ProgramU, studentBio.faculty = FacultyU, subjects.subjectName = SubjectNameU WHERE studentBio.RollNo = " + Convert.ToInt32(this.rollNumber7_combo.SelectedItem.ToString()) + " AND studentBio.RegYear = " + Convert.ToInt32(this.comboBox3.SelectedItem.ToString()) + " AND studentBio.program = '" + this.comboBox1.SelectedItem.ToString() + "' AND studentBio.faculty = '" + this.comboBox2.SelectedItem.ToString() + "'";
I've added the following parts to make it update subjects
table:
studentBio INNER JOIN subjects ON studentBio.RollNo = subjects.RollNo AND studentBio.RegYear = subjects.RegYear AND studentBio.program = subjects.program AND studentBio.faculty = subjects.faculty
- this part joins thesubjects
table making it's update possible, subjects.subjectName = SubjectNameU
- fragment in theSET
block updating subjectName column in subjects tableWHERE studentBio.RollNo = " + Convert.ToInt32(this.rollNumber7_combo.SelectedItem.ToString()) + " AND studentBio.RegYear = " + Convert.ToInt32(this.comboBox3.SelectedItem.ToString()) + " AND studentBio.program = '" + this.comboBox1.SelectedItem.ToString() + "' AND studentBio.faculty = '" + this.comboBox2.SelectedItem.ToString() + "'"
- the last part is theWHERE
block in which prefixstudentBio.
was added to each key column to precise to which table the filtering should be applied (because both tables ha the same key column names)
I hope it could help you in some way.
来源:https://stackoverflow.com/questions/27467545/how-to-perform-update-operation-on-two-tables-having-two-similar-fields-and-one