我有一个查询
$result = mysql_query("SELECT * FROM comprofiler WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY id ASC");
在该查询中,有一个user_id,我需要在另一个表上执行查询。
$gebruikerid = mysql_query("SELECT * FROM users where id LIKE '".$result['user_id']."'");
现在我想在while循环中使用该值
echo "<table><tr><th width=\"300\" align=\"left\" >Avatar</th><th width=\"300\" align=\"left\">Naam</th><th width=\"200\" align=\"left\">PSN Naam</th></tr>";
while($row2 = mysql_fetch_array($result))
{
echo "<tr><td><img height=\"50\" width=\"50\" src=\"/images/comprofiler/" . $row2['avatar'] . "\"></td><td>" . $gebruikernaam . "</td><td>" . $row2['cb_psnnaam'] . "</td></tr>";
}
echo "</table>";
我无法获取查询以根据第一个表的ID从另一个表中读取值。有人能帮我吗?
在您的情况下,最好的解决方案是使用JOIN。
$result = mysql_query("SELECT comprofiler.*, users.* FROM comprofiler INNER JOIN users ON users.id = comprofiler.user_id WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY comprofiler.id ASC");
如果两个表中的列名相同并且都需要,则可能必须手动指定要选择的列(在SELECT之后)。
$result = mysql_query("SELECT comprofiler.id as id_comprofiler, users.id as id_user, users.avatar ... " );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句