2016-11-19 29 views
0

我有一個連接表,它從我的受訪者表respondant_id中獲取ID,並從我的團隊表table_id獲取ID。從我的連接表中獲取數據

當我從該表中選擇時,輸出很好,所以我找回了與隊ID一起結尾的響應者ID。

我想用respondant_data的受訪者姓名和teams的團隊名稱通過使用連接表的輸出值來顯示受訪者姓名。

我試過這裏,但我一直得到0結果。

$sql = " 
SELECT 
respondant_data.respondant_id, teams.team_id 
FROM 
    respondant_data 
INNER JOIN 
    teams 
ON 
    respondant_data.respondant_id = teams.team_id 
WHERE 
    respondant_teams.team_id= 5"; 

$result = $conn->query($sql); 

$i = 1; 

if($result->num_rows > 0){ 
    while($row = $result->fetch_assoc()){ 
     echo $i++ . ' '; 
     echo 'user_id: ' . $row["respondant_id"] . ', '; 
     echo 'team_id: ' . $row["team_id"]; 
     echo '<br>';   
    } 
} else{ 
    echo 'no results'; 
} 

所以我想我的輸出要像「約翰·史密斯」,「中央隊」

+1

在一個地方你已經使用過團隊(第7,9行),並在一個地方使用過respondant_teams(第11行)。使用適當的表名。 –

+0

可能你正在加入WRONG鍵上的表....如果respondant_data有一個外鍵,比如team_id,那麼JOIN可以是:respondant_data.team_id = teams.team_id ....其中respondant_data表的team_id是一個外鍵指向隊表... – barudo

+0

Iv讓自己都感到困惑:(所以是的,我只是想加入我的中間表,其中有respondant_id和team_id並輸出了兄弟的名字和隊名也屬於... – PhpDude

回答

1

嘗試此查詢。

SELECT 
    resp_data.respondant_id, teams.team_id 
FROM 
    respondant_data resp_data, 
    teams, 
    respondant_teams resp_teams 
WHERE 
    resp_data.respondant_id = teams.team_id 
     and resp_teams.team_id = teams.team_id 
     and resp_teams.team_id = 5 
相關問題