1
卡住了!因此,我一直在研究如何根据另一个查询的结果运行查询并了解它效率不高(并且我没有得到我期望的结果)。依赖于另一个查询的Mysql查询与加入
这里是我的信息:
我有一个$ _Get变量为team_id。
我有两个表。表1和表2.表1具有team_id和player_id,而表2具有所有玩家信息。
我想获得表2中的player_name。看看我脑子里想的下方,以获得更好ID:
//assign and clean up tid
$tid = mysql_real_escape_string($_GET['tid']);
//query to find player id where the team id matches tid
$sql = "SELECT player_id FROM table1 WHERE team_id = '$tid'";
$result = mysql_query($sql);
//then I was basically going to run a while loop with another query to get the playerid
while ($row=mysql_fetch_array($result)){
$playerid=$row['player_id'];
$sql2 = "select player_name from table2 where player_id = '$playerid'";
$result2 = mysql_query($sql2);
echo $result2;
}
虽然这个研究我发现,似乎有一个concensus是用mysql连接是实现这一点没有while循环的最佳方式。我玩过这个选项,但并不真正了解如何加入工作。 我将如何创建一个实现我想要的结果的查询?
我看过那个,觉得非常有帮助。我迷路的地方是我如何确保只有那些符合特定团队ID的值才会被返回。即:我只希望队员身份都是=的球员姓名。所以如果team_id = = 7,那么我只希望表2中的队员的team_id是7。 – Rico 2011-04-18 17:54:31
GOT IT !!!猜猜想第二次帮助大声笑这里是我的代码:'$ sql =“SELECT * FROM PLAYERS INNER JOIN TEAM_PLAYERS ON TEAM_PLAYERS.PLAYER_ID = PLAYERS.PLAYER_ID”; $ result = mysql_query($ sql)或死(mysql_error()); 而($行= mysql_fetch_array($结果)){ \t如果($行[ 'TEAM_ID'] == $ TID){ \t \t回波$行[ 'PLAYER_NAME']; \t} }' – Rico 2011-04-18 18:10:45
这是皮肤的一种方式,但为了得到这个纯粹的SQL检查我的编辑。 – entropo 2011-04-18 18:49:39