2016-12-16 80 views
0

我有一張帶有導師列表的表格。一些導師有評級,有些則沒有。目前,我的查詢收集導師的基本信息和他們的評價信息。由於查詢正在收集評分信息,所以沒有評分的導師不會顯示在表格中,而只有具有評分的導師纔會顯示。有沒有解決的辦法?MySQL查詢不顯示錶中的所有結果。 INNER JOIN衝突

這裏是我的查詢:

$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"; 
} 

回答

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() 
+0

你簡直太神奇了!我一直在努力嘗試幾天,而左外連接節省了我的心思!非常感謝!這是一個很好的學習經歷,我想更多地研究這些聯結!謝謝!我會盡快給你答覆! :D:D – Millica

+0

'LEFT JOIN x WHERE x.n ...'與'INNER JOIN x ...'相同' – Strawberry