简体   繁体   中英

SQL query, JOIN another table syntax error

My tables:

Contents: content_id, publisher_id, title, category, trend

Users: id, name, rank

I am tring to JOIN to Users and get name and rank

My code:

$query = $conn->prepare
     ("select * from contents 
     FULL OUTER JOIN users ON contents.publisher_id = users.id 
     WHERE trend =1 order by content_id desc limit 20");
$query->execute(array());

};

$data = array();
while($result= $query->fetch(PDO::FETCH_ASSOC)) {
$data[] = array("content_id"    =>$result['content_id'], 
                "publisher_id"  =>$result['publisher_id'],
                "publisher_name"=>$result['name'],
                "publisher_rank"=>$result['rank],
                "title"         =>$result['title'],
                "category"      =>$result['category'],
                "type"          =>$result['type']
                );

};
echo json_encode($data);

Try this one. Hope this help

$query = $conn->prepare("select * from contents JOIN users ON contents.publisher_id = users.id 
     WHERE trend = 1 order by content_id desc limit 20");
$query->execute();

$data = array();
while($result= $query->fetch(PDO::FETCH_ASSOC)) {
$data[] = array("content_id"    =>$result['content_id'], 
                "publisher_id"  =>$result['publisher_id'],
                "publisher_name"=>$result['name'],
                "publisher_rank"=>$result['rank'],
                "title"         =>$result['title'],
                "category"      =>$result['category'],
                "type"          =>$result['type']
                );

};
echo json_encode($data);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM