Click here to Skip to main content
16,011,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

So, I have two tables, exam1 and exam2 which looks like these:

VB
table:exam1

sid       physics     chemistry       biology
3          80             60            40


VB
   table:exam2
sid        physics    chemistry       biology
3            40          50              70


[b]Problem:[/b]
I want to find the sum of items from both tables (exam1 and exam2), like this

VB
table: total
sid         physics         chemistry       biology
3            120               110            110


[b]BUT:[/b]
I wont always have to find total of all courses (physics, chemistry, biology), sometimes I may want to find sum of only two, one of all three subjects, so this is really a variable.

Since I program from VB.NET, the list of courses to be totaled can be specified by a user at run time and is kept in a variable say, "courses", so sometimes courses= (physics,chemistry,biology) sometimes courses=(physics,chemistry) and so on.

How can I then find a total of courses that the user selects? (still total of courses in exam1+exam2 tables)

Thanks

What I have tried:

<pre lang="vb">
C#
Dim courses As String = "physics,chemistry"

        Dim cmd As New MySqlCommand
        cmd.CommandText = "Select A.sid, (A." & LCase((courses)) & "+B." & LCase((courses)) &
                            ") As " & courses & " From exam1 A Inner Join exam2 B On A.sid=B.sid Order By A.sid"

        MsgBox("STATEMENT = " & cmd.CommandText)
        cmd.Connection = con
        Dim rd As MySqlDataReader = cmd.ExecuteReader()


        If rd.HasRows() Then
            Dim x As String
            Do While rd.Read()
                x &= String.Format("{0}  {1}", rd("sid"), rd(ComboBox2.SelectedItem)) & vbCrLf
            Loop
            MessageBox.Show(x)
        End If
        rd.Close()
        cmd.Dispose()
        con.Dispose()
Posted
Updated 26-Oct-16 11:00am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900