2014-04-07 78 views
0

我在嘗試使用帶有INNER JOIN的子查詢時遇到問題。使用INNER JOIN進行子查詢時遇到問題

在下面的代碼中,我試圖計算的平均評分僅需要該玩家(a.player)的平均評分爲

從看我下面的腳本,我的問題是

1)我應該在哪裏築巢內部採用當子查詢加入?

2)表格中有大約20個名字,我從這張表(大學表)中提取這些信息。在子查詢中,我需要'b.rating'來表示特定的玩家。我是否需要子查詢中的WHERE子句,如「WHERE player = a.player」?

$query = "SELECT a.player,a.team,a.loc,a.pic,a.rank,b.rating FROM college AS a 
     JOIN (SELECT AVG(rating) AS rating FROM college_rating) AS b 
     ON a.player=b.player 
     ORDER BY rank DESC LIMIT 20"; 
$run = mysqli_query($link,$query); 
while($row = mysqli_fetch_array($run)) { 
echo $row['player'] . ' ' . $row['rating'] . ' ' . $row['team'] . '<br>'; 
} 

回答

1

你需要找到的平均得分爲子查詢各在線播放如下圖所示

$query = "SELECT a.player,a.team,a.loc,a.pic,a.rank,b.rating FROM college AS a 
      JOIN (
       SELECT player,AVG(rating) AS rating 
       FROM college_rating 
       GROUP BY player 
       ) AS b 
     ON a.player=b.player 
     ORDER BY rank DESC LIMIT 20"; 
0

$查詢=「選擇a.player,a.team,a.loc,a.pic,一.rank ,(SELECT AVG(等級)FROM college_rating其中player = a.player GROUP BY player)作爲評級 FROM college a ORDER BY rank DESC LIMIT 20「;