Abhi Burk Abhi Burk - 3 months ago 11
MySQL Question

How Do I SELECT matched Values Of Session user to the the unsession user In PHP

I got two

table
one is
student
and another is
teacher
So Basically I want a Select Query for this to table such that If certain teacher teaches for Comp Sci Department and this record is saved in
teacher
table under
department
column and in another case certain student also studies in same department and his record is also saved in
student
table under
department
column So my question, i only want teacher to be selected whose department is equal to the login user

Table
student


studentID department
1 Comp Sci
2 Mech Engg


Table
teacher


teacherID department name
1 Comp Sci XYZ
2 Mech Engg ABC


In My Code it only selects the matched value from both table and i dont want this i dont know how to do that

<?php
$sql=("SELECT *
FROM student
LEFT JOIN teacher ON student.studentID=teacher .teacher ID
WHERE student.department=teacher .department ");
$res=mysql_query($sql) or die (mysql_error());
while($admin=mysql_fetch_assoc($res)) {
echo $admin['name'];
echo $admin['department'];
?>

Answer
SELECT * 
FROM 
    teacher 
INNER JOIN 
    student 
ON 
    student.department = teacher.department 
WHERE 
    student.studentID = $loginid // $login is the id of currently logged in student