How to perform UPDATE operation on two tables having two similar fields and one different field?

好久不见. 提交于 2020-01-07 05:48:31

问题


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:

AND in checkedlistbox1 i am showing corresponding subjects in the form like A+B+C. Which after retrieving from user would be split on the basis of '+' and be added into subjects table all at once.
  1. 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
    
  2. 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:

  1. 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 the subjects table making it's update possible

  2. , subjects.subjectName = SubjectNameU - fragment in the SET block updating subjectName column in subjects table

  3. 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() + "'" - the last part is the WHERE block in which prefix studentBio. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!