1

I am new to stack and c#. Here I created a c# windows form application. I am using SQL server with login and student tables. I store login details(UN,PW) in login table and general details of student(fname,lname,etc..) in student table.

What I want to get is First name of the student using his login details. ex- UN:- ravi, PW:- 123, fname:- Ravindu

But in my system, ALL the registered students are not users of the system and all the users are registered students

Should I link tables or any structured way to do this.

Anthony Horne
  • 2,522
  • 2
  • 29
  • 51
JLink
  • 307
  • 1
  • 6
  • 15

3 Answers3

1

You can use an outer or left join to represent everything in one table and only the matched items in the other.

select 
     * 
FROM 
     TABLEALL 
LEFT JOIN 
     TABLESOME 
ON 
     TABLEALL.ID = TABLESOME.ID
Anthony Horne
  • 2,522
  • 2
  • 29
  • 51
1

Yes, you need to link both tables via a ID. When all users are registered students, they should have data in Student table. If you login as Ravi as stated in your example, this user should be present in Student Table. You can create a loginID as Primary key in login table. This should be linked to Student table via a Foreign key loginID in Student table and you should be able to achieve what you are looking for.

select st.fname from login lin left join on student st lin.loginID =st.loginID where lin.loginID='ID of student';

user2965957
  • 17
  • 10
1

Since you are working with student table, i assume that every student has unique index number, so you could add that unique Index column in student table and in Index column your login table, since all of your system users are students...

SELECT studentName FROM studentTable
WHERE Index IN
                (
                    SELECT Index FROM loginTable 
                    WHERE loginTable.userName = username 
                    AND loginTable.password = password
                )
dgilperez
  • 10,716
  • 8
  • 68
  • 96