How to select rows in table UsrArticle which UsrId = 1? I'm new to use join, below is my code is there something wrong it only print empty array?
table Usr
id UsrId
0 0
1 1
2 2
3 3
table UsrArticle
ArticleId UsrId
0 1
1 0
2 1
3 1
4 1
5 3
php
$UsrId = 1;
$sth = $db->prepare("SELECT * FROM Usr LEFT JOIN UsrArticle ON (Usr.UsrId = UsrArticle.UsrId) WHERE UsrId = :UsrId");
$sth->bindParam(':UsrId',$UsrId,PDO::PARAM_INT);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
$ArticleRows = $sth->fetchAll();
print_r($ArticleRows);
It will be failing because UsrID
is ambiguous (there are multiple possibilities for which column you are referring to). I know it's silly because your conditions are specifying that they should be the same, but that's how SQL works. Add a table alias to the prefix of that field name:
... WHERE Usr.UsrId = :UsrId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.