MySQL查询未在表中显示所有结果。 INNER JOIN冲突

时间:2016-12-16 22:29:35

标签: mysql

我有一张包含导师名单的表格。一些导师有收视率,有些没有。目前,我的查询收集了导师'基本信息以及他们的评级信息。因为查询正在收集评级信息,所以没有评级的导师没有显示在表格中,而只有具有评级的导师。有办法解决这个问题吗?

这是我的疑问:

$sql = "SELECT users.id, users.firstName, users.lastName, users.username, users.gender, users.avatar, mentor.industry, mentor.city, mentor.price, mentor.language, users.country, mentor.mentor_enabled, AVG(rating.rate) AS average, COUNT(rate) AS count FROM users INNER JOIN mentor ON users.id = mentor.id INNER JOIN rating ON mentor.id = rating.owner WHERE mentor.mentor_enabled='1' GROUP BY users.id, users.username ORDER BY RAND()";
$res = mysqli_query($db,$sql) or die(mysqli_error());               
if (mysqli_num_rows($res) > 0) {
    while ($row=mysqli_fetch_assoc($res)){
        $id = $row['id'];
        //NAMING VARIABLES
    }
 } else{
  echo "no data";
}

1 个答案:

答案 0 :(得分:0)

您需要使用left outer join代替inner join来获取所有行,例如:

SELECT users.id, users.firstName, users.lastName, users.username, users.gender, users.avatar, mentor.industry, mentor.city, mentor.price, mentor.language, users.country, mentor.mentor_enabled, AVG(rating.rate) AS average, COUNT(rate) AS count 
FROM users LEFT OUTER JOIN mentor ON users.id = mentor.id 
INNER JOIN rating ON mentor.id = rating.owner 
WHERE mentor.mentor_enabled='1' 
GROUP BY users.id, users.username 
ORDER BY RAND()