how to join up database table

KwebenaAcquah-9104 346 Reputation points
2021-06-01T03:07:03.933+00:00

i am trying to join 6 tables;
such as shown in this code below and show all in one datagridview
from sql database;

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. cheong00 3,486 Reputation points Volunteer Moderator
    2021-06-01T03:17:47.843+00:00

    The join condition ("ON part") should be immediately following the corresponding "JOIN" part.

                     string sql = "SELECT s1.FirstName, s1.LastName, " +
                         "s2.EXAMSMATHEMATICS " +
                         "s3.TOTALMATHEMATICS" +
                         "s4.OVERALLTOTALMATHEMATICS" +
                         "s5.POSITIONMATHEMATICS," +
                         "s6.GRADEMATHEMATICS " +
                         "FROM(SELECT FirstName, LastName,ROW_NUMBER() OVER(ORDER BY FirstName)As rn1 FROM tbl_TestingTheApplicationsNAME)as s1 " +
                         "FULL OUTER JOIN(SELECT EXAMSMATHEMATICS,ROW_NUMBER() OVER (ORDER BY EXAMSMATHEMATICS)As rn2 FROM tbl_EXAMSSCORES)as s2 ON s1.rn1 = s2.rn2" +
                         "FULL OUTER JOIN(SELECT TOTALMATHEMATICS,ROW_NUMBER() OVER (ORDER BY TOTALMATHEMATICS)As rn3 FROM tbl_TOTALSCORES)as s3 ON s2.rn2 = s3.rn3" +
                         "FULL OUTER JOIN(SELECT OVERALLTOTALMATHEMATICS,ROW_NUMBER() OVER (ORDER BY OVERALLTOTALMATHEMATICS)As rn4 FROM tbl_OVERALLSCORES)as s4 ON s3.rn3 = s4.rn4" +
                         "FULL OUTER JOIN(SELECT POSITIONMATHEMATICS,ROW_NUMBER() OVER (ORDER BY POSITIONMATHEMATICS)As rn5 FROM tbl_POSITIONSCORES)as s5 ON s4.rn4 = s5.rn5" +
                         "FULL OUTER JOIN(SELECT GRADEMATHEMATICS,ROW_NUMBER() OVER (ORDER BY GRADEMATHEMATICS)As rn6 FROM tbl_GRADESCORE)as s6 ON s5.rn5 = s6.rn6";
    

    Also note that your join condition doesn't make much sense because except rn1 which is over "FirstName", other ROW_NUMBER() are ordered by score fields.
    Use some primary key fields for joining instead. There ought to be things like "Student Number" which should be unique key field for students.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.