public function getUserList() {
$sql = "SELECT users.id, users.name, about.profile_img, user_game_info.at_game, SUM(user_img.likes) AS likes FROM users
JOIN about ON users.id = about.uid
JOIN user_game_info ON about.uid = user_game_info.uid
INNER JOIN user_img ON user_game_info.uid = user_img.uid";
$query = $this->db->query($sql, PDO::FETCH_ASSOC);
if ($query->rowCount()) {
$userList = $query->fetchAll();
return $userList;
}
return false;
}
它可以工作,但如果我的user_img没有uid,我想显示喜欢计数0。 仅当img_user具有uid时才返回此查询,如果没有uid,则返回该查询。什么都没有。
这仅适用于user_img。例如,对于uid 3,该查询必须返回0.因为user_img没有uid = 3.但它不返回uid 3的任何数据。
答案 0 :(得分:2)
确定我已经更新了SQL,请再次尝试:
您需要左连接:
$sql = '
SELECT u.id, u.name, a.profile_img, ugi.at_game, SUM(ifnull(i.likes,0)) AS likes
FROM users u
LEFT JOIN about a ON u.id = a.uid
LEFT JOIN user_game_info ugi ON u.id = ugi.uid
LEFT JOIN user_img i ON u.id = i.uid
GROUP BY 1';