Reputation: 9066
i am trying to write my first join query which will help me to join two different tables,one is for students and other is for subjects.This is my first join query and it seems it is not working,can anyone point out the mistakes.Php didn't show any error or worning
try{
$pdo=new PDO("mysql:host=localhost;dbname=mydb",'root','');
$sql="SELECT * FROM students LEFT JOIN subjects WHERE students.courseid=subjects.courseid";
$conn=$pdo->prepare($sql);
if($conn->execute()){
$results=$conn->fetchAll();
print_r($results);
}
}catch(PDOException $e){
echo $e->getMessage();
}
Upvotes: 3
Views: 81
Reputation: 423
Replace WHERE
with ON
in your query.
SELECT * FROM students LEFT JOIN subjects ON students.courseid=subjects.courseid
Upvotes: 2
Reputation: 6133
Assuming you table name for students is students and you subjects table is subject then I think it should be:
SELECT * FROM students LEFT JOIN subjects on students.courseid=subjects.subjectid
because you have a subjectid in your subjects table and not courseid
Upvotes: 2