I want to select information from two SQL tables within one query.
table1
id | postID | status | number | userID | active
1 | 1 | 100 | 100 | 3 | 1
2 | 7 | 50 | 25 | 5 | 1
3 | 3 | 75 | 50 | 3 | 1
table2
postID | reference | joint_date | userID | remove
1 | 100 | 100 | 3 | 1
2 | 50 | 25 | 5 | 1
3 | 50 | 50 | 3 | 0
Expected Output
postID | status | number | reference | joint_date
1 | 100 | 100 | 100 | 100
3 | 75 | 50 | 50 | 50
This is my try:
$userID = 12;
$active = 1;
$remove= 1;
$sql = "SELECT table1.status, table1.number, table2.reference, table2.joint_date
FROM table1
WHERE table1.active=:active AND table1.userID=:userID
INNER JOIN table2 ON table1.postID=table2.postID
WHERE table2.postID=:userID
AND table2.remove=:remove
ORDER BY table1.postID DESC";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(":active", $active, PDO::PARAM_INT);
$stmt->bindValue(":userID", $userID, PDO::PARAM_INT);
$stmt->bindValue(":remove", $remove, PDO::PARAM_INT);
$stmt->execute();
What is wrong with my query?